Wait Method (Int32, CancellationToken)

Task::Wait Method (Int32, CancellationToken)

.NET Framework (current version)
 

Waits for the Task to complete execution. The wait terminates if a timeout interval elapses or a cancellation token is canceled before the task completes.

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

public:
bool Wait(
	int millisecondsTimeout,
	CancellationToken cancellationToken
)

Parameters

millisecondsTimeout
Type: System::Int32

The number of milliseconds to wait, or Infinite (-1) to wait indefinitely.

cancellationToken
Type: System.Threading::CancellationToken

A cancellation token to observe while waiting for the task to complete.

Return Value

Type: System::Boolean

true if the Task completed execution within the allotted time; otherwise, false.

Exception Condition
OperationCanceledException

The cancellationToken was canceled.

ObjectDisposedException

The Task has been disposed.

ArgumentOutOfRangeException

millisecondsTimeout is a negative number other than -1, which represents an infinite time-out.

AggregateException

The task was canceled. The AggregateException::InnerExceptions collection contains a TaskCanceledException object.

-or-

An exception was thrown during the execution of the task. The AggregateException::InnerExceptions collection contains information about the exception or exceptions.

Wait(Int32, CancellationToken) is a synchronization method that causes the calling thread to wait for the current task instance to complete until one of the following occurs:

  • The task completes successfully.

  • The task itself is canceled or throws an exception. In this case, you handle an AggregateException exception. The AggregateException::InnerExceptions property contains details about the exception or exceptions.

  • The cancellationToken cancellation token is canceled. In this case, the call to the Wait(Int32, CancellationToken) method throws an OperationCanceledException.

  • The interval defined by millisecondsTimeout elapses. In this case, the current thread resumes execution and the method returns false.

System_CAPS_noteNote

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 based on some condition.

The following example calls the Wait(Int32, CancellationToken) method to provide both a timeout value and a cancellation token that can end the wait for a task's completion. A new thread is started and executes the CancelToken method, which pauses and then calls the CancellationTokenSource::Cancel method to cancel the cancellation tokens. A task is then launched and delays for 5 seconds. The Wait method is then called to wait for the task's completion and is provided both a brief timeout value and a cancellation token.

No code example is currently available or this language may not be supported.

Note that the precise output from the example depends on whether the wait was canceled because of the cancellation token or because the timeout interval elapsed.

Universal Windows Platform
Available since 4.5
.NET Framework
Available since 4.0
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 5.0
Windows Phone Silverlight
Available since 8.0
Windows Phone
Available since 8.1
Return to top
Show:
© 2016 Microsoft