Task.Delay Method (TimeSpan, CancellationToken)

.NET Framework (current version)

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Creates a cancellable task that completes after a specified time interval.

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

Public Shared Function Delay (
	delay As TimeSpan,
	cancellationToken As CancellationToken
) As Task


Type: System.TimeSpan

The time span to wait before completing the returned task, or TimeSpan.FromMilliseconds(-1) to wait indefinitely.

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.

Exception Condition

delay represents a negative time interval other than TimeSpan.FromMillseconds(-1).


The delay argument's TotalMilliseconds property is greater than Int32.MaxValue.


The task has been canceled.


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 delay 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(TimeSpan, CancellationToken) method with a one-and-a-half 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.

Imports System.Threading
Imports System.Threading.Tasks

Module Example
   Public Sub Main()
      Dim source As New CancellationTokenSource()

      Dim t = Task.Run(Async Function()
                                Await Task.Delay(TimeSpan.FromSeconds(1.5),
                                Return 42
                       End Function)
      Catch ae As AggregateException
         For Each e In ae.InnerExceptions
            Console.WriteLine("{0}: {1}", e.GetType().Name, e.Message)
      End Try
      Console.Write("Task t Status: {0}", t.Status)
      If t.Status = TaskStatus.RanToCompletion Then
         Console.Write(", Result: {0}", t.Result)
      End If
   End Sub
End Module
' The example displays output like the following:
'       TaskCanceledException: A task was canceled.
'       Task t Status: Canceled

Note that this example includes a potential race condition: it depends on the task asynchronously executing the delay when the token is cancelled. Although the 1.5 second delay from the call to the Delay(TimeSpan, CancellationToken) method makes that assumption likely, it is nevertheless possible that the call to the Delay(TimeSpan, CancellationToken) method could return before the token is cancelled. In that case, the example produces the following output:

Task t Status: RanToCompletion, Result: 42

Universal Windows Platform
Available since 8
.NET Framework
Available since 4.5
Portable Class Library
Supported in: portable .NET platforms
Windows Phone Silverlight
Available since 8.0
Windows Phone
Available since 8.1
Return to top