Task.Delay Method (Int32)

.NET Framework 4.5

Creates a task that completes after a time delay.

Namespace:  System.Threading.Tasks
Assembly:  mscorlib (in mscorlib.dll)

public static Task Delay(
	int millisecondsDelay
)

Parameters

millisecondsDelay
Type: System.Int32

The number of milliseconds to wait before completing the returned task, or -1 to wait indefinitely.

Return Value

Type: System.Threading.Tasks.Task
A task that represents the time delay.

ExceptionCondition
ArgumentOutOfRangeException

The millisecondsDelay argument is less than -1.

The Delay method is typically used to delay the operation of all or part of a task for a specified time interval. Most commonly, the time delay is introduced:

  • At the beginning of the task, as the following example shows.

    Stopwatch sw = Stopwatch.StartNew();
    var delay = Task.Delay(1000).ContinueWith(_ =>
                               { sw.Stop();
                                 return sw.ElapsedMilliseconds; } );
    
    Console.WriteLine("Elapsed milliseconds: {0}", delay.Result);
    // The example displays output like the following: 
    //        Elapsed milliseconds: 1013
    
  • Sometime while the task is executing. In this case, the call to the Delay method executes as a child task within a task, as the following example shows. Note that since the task that calls the Delay method executes asynchronously, the parent task must wait for it to complete by calling the Wait method.

    var delay = Task.Run( () => { Stopwatch sw = Stopwatch.StartNew();
                                  Task.Delay(2000).Wait();
                                  sw.Stop();
                                  return sw.ElapsedMilliseconds; });
    
    Console.WriteLine("Elapsed milliseconds: {0}", delay.Result);
    // The example displays output like the following: 
    //        Elapsed milliseconds: 2006
    

    This example can also be written using the async and await keywords available in C# and Visual Basic:

    var delay = Task.Run( async () => { Stopwatch sw = Stopwatch.StartNew();
                                        await Task.Delay(2500);
                                        sw.Stop();
                                        return sw.ElapsedMilliseconds; });
    
    Console.WriteLine("Elapsed milliseconds: {0}", delay.Result);
    // The example displays output like the following: 
    //        Elapsed milliseconds: 2006
    

After the specified time delay, the task is completed in the TaskStatus.RanToCompletion state.

This method depends on the system clock. This means that the time delay will approximately equal the resolution of the system clock if the millisecondsDelay argument is less than the resolution of the system clock, which is approximately 15 milliseconds on Windows systems.

The following example shows a simple use of the Delay method.

using System;
using System.Threading.Tasks;

public class Example
{
   public static void Main()
   {
      var t = Task.Run(async delegate
              {
                 await Task.Delay(1000);
                 return 42;
              });
      t.Wait();
      Console.WriteLine("Task t Status: {0}, Result: {1}",
                        t.Status, t.Result);
   }
}
// The example displays the following output: 
//        Task t Status: RanToCompletion, Result: 42

.NET Framework

Supported in: 4.6, 4.5

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2015 Microsoft