Creates a cancellable task that completes after a specified time interval.
public:
static System::Threading::Tasks::Task ^ Delay(TimeSpan delay, System::Threading::CancellationToken cancellationToken);
public static System.Threading.Tasks.Task Delay(TimeSpan delay, System.Threading.CancellationToken cancellationToken);
static member Delay : TimeSpan * System.Threading.CancellationToken -> System.Threading.Tasks.Task
Public Shared Function Delay (delay As TimeSpan, cancellationToken As CancellationToken) As Task
Parameters
The time span to wait before completing the returned task, or Timeout.InfiniteTimeSpan
to wait indefinitely.
A cancellation token to observe while waiting for the task to complete.
ReturnsA task that represents the time delay.
Exceptionsdelay
represents a negative time interval other than Timeout.InfiniteTimeSpan
.
-or-
The delay
argument's TotalMilliseconds property is greater than 4294967294 on .NET 6 and later versions, or Int32.MaxValue on all previous versions.
The task has been canceled. This exception is stored into the returned task.
The provided cancellationToken
has already been disposed.
The task has been canceled.
ExamplesThe following example launches a task that includes a call to the Delay(TimeSpan, CancellationToken) method with a 1.5 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 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(TimeSpan.FromSeconds(1.5), 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 output like the following:
// TaskCanceledException: A task was canceled.
// Task t Status: Canceled
open System
open System.Threading
open System.Threading.Tasks
let source = new CancellationTokenSource()
let t =
Task.Run<int>(fun () ->
task {
do! Task.Delay(TimeSpan.FromSeconds(1.5), source.Token)
return 42
})
source.Cancel()
try
t.Wait()
with :? AggregateException as ae ->
for e in ae.InnerExceptions do
printfn $"{e.GetType().Name}: {e.Message}"
printf $"Task t Status: {t.Status}"
if t.Status = TaskStatus.RanToCompletion then
printf $", Result: {t.Result}"
source.Dispose()
// The example displays output like the following:
// TaskCanceledException: A task was canceled.
// Task t Status: 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),
source.Token)
Return 42
End Function)
source.Cancel()
Try
t.Wait()
Catch ae As AggregateException
For Each e In ae.InnerExceptions
Console.WriteLine("{0}: {1}", e.GetType().Name, e.Message)
Next
End Try
Console.Write("Task t Status: {0}", t.Status)
If t.Status = TaskStatus.RanToCompletion Then
Console.Write(", Result: {0}", t.Result)
End If
source.Dispose()
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
Remarks
If the cancellation token is signaled before the specified time delay, a TaskCanceledException exception results, and the task is completed in the Canceled state. Otherwise, the task is completed in the 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.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4