Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Task.Delay Method (Int32, CancellationToken)

.NET Framework 4.6 and 4.5

Creates a cancellable task that completes after a time delay.

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

public static Task Delay(
	int millisecondsDelay,
	CancellationToken cancellationToken
)

Parameters

millisecondsDelay
Type: System.Int32

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

cancellationToken
Type: System.Threading.CancellationToken

The cancellation token that will be checked prior to completing the returned task.

Return Value

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

ExceptionCondition
ArgumentOutOfRangeException

The millisecondsDelay argument is less than -1.

TaskCanceledException

The task has been canceled.

ObjectDisposedException

The provided cancellationToken has already been disposed.

If the cancellation token is signaled before the specified time delay, a TaskCanceledException exception results, and the task is completed in the TaskStatus.Canceled state. Otherwise, the task is completed in the TaskStatus.RanToCompletion state once the specified time delay has elapsed.

For usage scenarios and additional examples, see the documentation for the Delay(Int32) overload.

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 launches a task that includes a call to the Delay(Int32, CancellationToken) method with a one second delay. Before the delay interval elapses, the token is cancelled. The output from the example shows that, as a result, a TaskCanceledException is thrown, and the tasks' Status property is set to TaskStatus.Canceled.

using System;
using System.Threading;
using System.Threading.Tasks;

public class Example
{
   public static void Main()
   {
      CancellationTokenSource source = new CancellationTokenSource();

      var t = Task.Run(async delegate
              {
                 await Task.Delay(1000, source.Token);
                 return 42;
              });
      source.Cancel();
      try {
         t.Wait();
      }
      catch (AggregateException ae) {
         foreach (var e in ae.InnerExceptions)
            Console.WriteLine("{0}: {1}", e.GetType().Name, e.Message);
      }
      Console.Write("Task t Status: {0}", t.Status);
      if (t.Status == TaskStatus.RanToCompletion)
         Console.Write(", Result: {0}", t.Result);
      source.Dispose();
   }
}
// The example displays the following output: 
//       TaskCanceledException: A task was canceled. 
//       Task t Status: Canceled

.NET Framework

Supported in: 4.6, 4.5

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8
Show:
© 2015 Microsoft