Repeating Tasks
-
Wednesday, October 12, 2011 6:13 PM
Hi!
Is there any common approach for periodical tasks execution? I need to
perform task periodically (modelling hardware, some kind of
state-machine). Here is one soultion:
http://stackoverflow.com/questions/4890915/is-there-a-task-based-replacement-for-system-threading-timer
But maybe there exists another more fine-grained solution? Custom
TaskScheduler? Is there any possibility to continue task with itself?
God bless you!
All Replies
-
Wednesday, October 12, 2011 7:00 PM
We are introducing Task.Delay() in .NET 4.5 - http://msdn.microsoft.com/en-us/library/hh194865(v=VS.110).aspx. You continue from the result of Task.Delay() with the actual task that you want to execute, or you can do "await Task.Delay(...); <continuation code>...".
This posting is provided "AS IS" with no warranties, and confers no rights. -
Wednesday, October 12, 2011 7:13 PMThanks for rapid response. Can you please provide us with some code sample of how can I use this in my scenario?
God bless you! -
Wednesday, October 12, 2011 9:55 PM
Try this:
var cts = new CancellationTokenSource(5000); // Auto-cancel after 5 sec
Action<Task> repeatAction = null;
repeatAction = _ignored1 =>
{
Console.WriteLine("{0}: Repeating every 1 sec", DateTime.Now);
Task.Delay(1000, cts.Token).ContinueWith(_ignored2 => repeatAction(_ignored2), cts.Token); // Repeat every 1 sec
};
Console.WriteLine("{0}: Launching with 2 sec delay", DateTime.Now);
Task.Delay(2000, cts.Token).ContinueWith(repeatAction, cts.Token); // Launch with 2 sec delay
cts.Token.WaitHandle.WaitOne(); // Wait for the CTS to get canceled. Must be after 5 sec.
Console.WriteLine("{0}: Done after 5 sec of recurring", DateTime.Now);
On my machine it produces the expected output:
2011-10-12 14:52:06: Launching with 2 sec delay
2011-10-12 14:52:08: Repeating every 1 sec
2011-10-12 14:52:09: Repeating every 1 sec
2011-10-12 14:52:10: Repeating every 1 sec
2011-10-12 14:52:11: Repeating every 1 sec
2011-10-12 14:52:11: Done after 5 sec of recurring
Zlatko Michailov
Software Development Engineer, Parallel Computing Platform
Microsoft Corp.
This posting is provided "AS IS" with no warranties, and confers no rights.- Proposed As Answer by Zlatko Michailov - MSFT Wednesday, October 12, 2011 9:55 PM
- Marked As Answer by tillias Thursday, October 13, 2011 4:05 AM
-
Wednesday, October 12, 2011 11:25 PM
Here is a nicer version that uses await:
static void Example2()
{
var cts = new CancellationTokenSource(5000); // Work for 5 sec
try
{
DelayAsync(2000, 1000, cts.Token).Wait();
}
catch (AggregateException ae)
{
ae.Handle(e => e is TaskCanceledException);
}
Console.WriteLine("{0}: Done after 5 sec of recurring", DateTime.Now);
}
static async Task DelayAsync(int delayMillis, int repeatMillis, CancellationToken ct)
{
Console.WriteLine("{0}: Launching with 2 sec delay", DateTime.Now);
await Task.Delay(delayMillis, ct);
while (true)
{
Console.WriteLine("{0}: Repeating every 1 sec", DateTime.Now);
await Task.Delay(repeatMillis, ct);
}
}
Zlatko Michailov
Software Development Engineer, Parallel Computing Platform
Microsoft Corp.
This posting is provided "AS IS" with no warranties, and confers no rights.- Proposed As Answer by Zlatko Michailov - MSFT Wednesday, October 12, 2011 11:25 PM
- Marked As Answer by tillias Thursday, October 13, 2011 4:05 AM

