TaskExtensions Class

Provides extension methods for Task and Task<TResult> objects.

public static class TaskExtensions

Methods

NameDescription
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

this Task
The task to wait for.
deadline Deadline
The deadline at which the wait gives up.
cancellationToken CancellationToken
A CancellationToken that, when signaled, ends the wait.

Returns

Task A Task that represents the wait, and completes as this does.

Exceptions

ArgumentNullException
this is null.
TimeoutException
deadline expired before this completed.
OperationCanceledException
cancellationToken was 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

this Task<TResult>
The task to wait for.
deadline Deadline
The deadline at which the wait gives up.
cancellationToken CancellationToken
A CancellationToken that, 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

ArgumentNullException
this is null.
TimeoutException
deadline expired before this completed.
OperationCanceledException
cancellationToken was 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.