回答済み Repeating Tasks

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 PM
     
     
    Thanks 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
     
     Answered

    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.
  • Wednesday, October 12, 2011 11:25 PM
     
     Answered

    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
    •