Task.Delay Method (Int32)

.NET Framework (current version)
 
System_CAPS_noteNote

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 task that completes after a time delay.

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

Public Shared Function Delay (
	millisecondsDelay As Integer
) As Task

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.

Exception Condition
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.

    Dim sw As Stopwatch = Stopwatch.StartNew()
    Dim delay1 = Task.Delay(1000)
    Dim delay2 = delay1.ContinueWith( Function(antecedent)
                            sw.Stop()
                            Return sw.ElapsedMilliseconds
                          End Function)
    
    Console.WriteLine("Elapsed milliseconds: {0}", delay2.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 using the await keyword.

    Dim delay = Task.Run( Async Function()
                             Dim sw As Stopwatch = Stopwatch.StartNew()
                             Await Task.Delay(2500)
                             sw.Stop()
                             Return sw.ElapsedMilliseconds
                          End Function )
    
    Console.WriteLine("Elapsed milliseconds: {0}", delay.Result)
    ' The example displays output like the following:
    '        Elapsed milliseconds: 2501
    

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.

Imports System.Threading.Tasks

Module Example
   Public Sub Main()
      Dim t = Task.Run(Async Function()
                                Await Task.Delay(1000)
                                Return 42
                       End Function)
      t.Wait()
      Console.WriteLine("Task t Status: {0}, Result: {1}",
                        t.Status, t.Result)
   End Sub
End Module
' The example displays 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
Show: