TaskExtensions Class
Provides extension methods for Task and Task<TResult> objects.
public static class TaskExtensionsMethods
| Name | Description |
|---|---|
| Task.WaitAsync(Deadline, CancellationToken) | Asynchronously waits for this task to complete, until a specified deadline expires. |
| Task<TResult>.WaitAsync(Deadline, CancellationToken) | Asynchronously waits for this task to complete, until a specified deadline expires. |
Task.WaitAsync(Deadline, CancellationToken)
Asynchronously waits for this task to complete, until a specified deadline expires.
extension(Task @this)
{
public Task WaitAsync(Deadline deadline, CancellationToken cancellationToken = default)
}Parameters
thisTask- The task to wait for.
deadlineDeadline- The deadline at which the wait gives up.
cancellationTokenCancellationToken- A
CancellationTokenthat, when signaled, ends the wait.
Returns
Task A Task that represents the wait, and completes as this does.
Exceptions
ArgumentNullExceptionthisisnull.TimeoutExceptiondeadlineexpired beforethiscompleted.OperationCanceledExceptioncancellationTokenwas signaled before the wait ended.
Remarks
This method behaves as Task.WaitAsync does with the Deadline.RemainingTime of deadline as its timeout, on the clock of deadline. When the wait ends with a TimeoutException or an OperationCanceledException, this keeps running.
If this is complete when this method is called, so is the returned task, even if deadline has expired or cancellationToken is signaled. Otherwise, if cancellationToken is signaled when this method is called, the returned task is canceled, even if deadline has expired.
This method waits on a timer of the clock, which it sets to a whole number of milliseconds. The timer can fire up to one millisecond after deadline expires, or more when it is late.
If more than Int32.MaxValue milliseconds, about 24.8 days, remain until deadline expires, this method starts no timer. The wait then ends only when this completes or cancellationToken is signaled.
Task<TResult>.WaitAsync(Deadline, CancellationToken)
Asynchronously waits for this task to complete, until a specified deadline expires.
extension<TResult>(Task<TResult> @this)
{
public Task<TResult> WaitAsync(Deadline deadline, CancellationToken cancellationToken = default)
}Type parameters
TResult- The type of the result of
this.
Parameters
thisTask<TResult>- The task to wait for.
deadlineDeadline- The deadline at which the wait gives up.
cancellationTokenCancellationToken- A
CancellationTokenthat, when signaled, ends the wait.
Returns
Task<TResult> A Task<TResult> that represents the wait, and completes as this does, with the same result.
Exceptions
ArgumentNullExceptionthisisnull.TimeoutExceptiondeadlineexpired beforethiscompleted.OperationCanceledExceptioncancellationTokenwas signaled before the wait ended.
Remarks
This method behaves as Task.WaitAsync does with the Deadline.RemainingTime of deadline as its timeout, on the clock of deadline. When the wait ends with a TimeoutException or an OperationCanceledException, this keeps running.
If this is complete when this method is called, so is the returned task, even if deadline has expired or cancellationToken is signaled. Otherwise, if cancellationToken is signaled when this method is called, the returned task is canceled, even if deadline has expired.
This method waits on a timer of the clock, which it sets to a whole number of milliseconds. The timer can fire up to one millisecond after deadline expires, or more when it is late.
If more than Int32.MaxValue milliseconds, about 24.8 days, remain until deadline expires, this method starts no timer. The wait then ends only when this completes or cancellationToken is signaled.