Task::Wait Method (CancellationToken)
Waits for the Task to complete execution. The wait terminates if a cancellation token is canceled before the task completes.
Assembly: mscorlib (in mscorlib.dll)
The cancellationToken was canceled.
The task has been disposed.
Themethod creates a cancelable wait; that is, it causes the current thread to wait until one of the following occurs:
The task completes.
The cancellation token is canceled. In this case, the call to the OperationCanceledException.method throws an
Canceling the cancellationToken cancellation token has no effect on the running task unless it has also been passed the cancellation token and is prepared to handle cancellation. Passing the cancellationToken object to this method simply allows the wait to be canceled.
The following example illustrates the simple use of a cancellation token to cancel waiting for a task's completion. A task is launched, calls the CancellationTokenSource::Cancel method to cancel any of the token source's cancellation tokens, and then delays for five seconds. Note that the task itself has not been passed the cancellation token and is not cancelable. The application thread calls the task's method to wait for the task to complete, but the wait is canceled once the cancellation token is cancelled and an OperationCanceledException is thrown. The exception handler reports the exception and then sleeps for six seconds. As the output from the example shows, that delay allows the task to complete in the TaskStatus::RanToCompletion state.
Available since 8
Available since 4.0
Portable Class Library
Supported in: portable .NET platforms
Available since 5.0
Windows Phone Silverlight
Available since 8.0
Available since 8.1