TplExtensions.ToTask(WaitHandle, Int32, CancellationToken) Method

Definition

Creates a TPL Task that returns true when a WaitHandle is signaled or returns false if a timeout occurs first.

public static System.Threading.Tasks.Task<bool> ToTask (this System.Threading.WaitHandle handle, int timeout = -1, System.Threading.CancellationToken cancellationToken = default);
static member ToTask : System.Threading.WaitHandle * int * System.Threading.CancellationToken -> System.Threading.Tasks.Task<bool>
<Extension()>
Public Function ToTask (handle As WaitHandle, Optional timeout As Integer = -1, Optional cancellationToken As CancellationToken = Nothing) As Task(Of Boolean)

Parameters

handle
WaitHandle

The handle whose signal triggers the task to be completed. Do not use a Mutex here.

timeout
Int32

The timeout (in milliseconds) after which the task will return false if the handle is not signaled by that time.

cancellationToken
CancellationToken

A token whose cancellation will cause the returned Task to immediately complete in a canceled state.

Returns

A Task that completes when the handle is signaled or times out, or when the caller's cancellation token is canceled. If the task completes because the handle is signaled, the task's result is true. If the task completes because the handle is not signaled prior to the timeout, the task's result is false.

Remarks

The completion of the returned task is asynchronous with respect to the code that actually signals the wait handle.

Applies to