SemaphoreSlim.WaitAsync Method

Definition

Asynchronously waits to enter the SemaphoreSlim.

Overloads

WaitAsync(Int32, CancellationToken)

Asynchronously waits to enter the SemaphoreSlim, using a 32-bit signed integer to measure the time interval, while observing a CancellationToken.

WaitAsync()

Asynchronously waits to enter the SemaphoreSlim.

WaitAsync(Int32)

Asynchronously waits to enter the SemaphoreSlim, using a 32-bit signed integer to measure the time interval.

WaitAsync(CancellationToken)

Asynchronously waits to enter the SemaphoreSlim, while observing a CancellationToken.

WaitAsync(TimeSpan)

Asynchronously waits to enter the SemaphoreSlim, using a TimeSpan to measure the time interval.

WaitAsync(TimeSpan, CancellationToken)

Asynchronously waits to enter the SemaphoreSlim, using a TimeSpan to measure the time interval, while observing a CancellationToken.

WaitAsync(Int32, CancellationToken)

Asynchronously waits to enter the SemaphoreSlim, using a 32-bit signed integer to measure the time interval, while observing a CancellationToken.

public:
 System::Threading::Tasks::Task<bool> ^ WaitAsync(int millisecondsTimeout, System::Threading::CancellationToken cancellationToken);
public System.Threading.Tasks.Task<bool> WaitAsync (int millisecondsTimeout, System.Threading.CancellationToken cancellationToken);
member this.WaitAsync : int * System.Threading.CancellationToken -> System.Threading.Tasks.Task<bool>
Public Function WaitAsync (millisecondsTimeout As Integer, cancellationToken As CancellationToken) As Task(Of Boolean)

Parameters

millisecondsTimeout
Int32

The number of milliseconds to wait, Infinite (-1) to wait indefinitely, or zero to test the state of the wait handle and return immediately.

cancellationToken
CancellationToken

The CancellationToken to observe.

Returns

A task that will complete with a result of true if the current thread successfully entered the SemaphoreSlim, otherwise with a result of false.

Exceptions

millisecondsTimeout is a number other than -1, which represents an infinite timeout.

-or-

millisecondsTimeout is greater than Int32.MaxValue.

The current instance has already been disposed.

cancellationToken was canceled.

Remarks

If the timeout is set to -1 milliseconds, the method waits indefinitely.

If the timeout is set to zero milliseconds, the method doesn't block. It tests the state of the wait handle and returns immediately.

This method stores in the task it returns all non-usage exceptions that the method's synchronous counterpart can throw. If an exception is stored into the returned task, that exception will be thrown when the task is awaited. Usage exceptions, such as ArgumentException, are still thrown synchronously. For the stored exceptions, see the exceptions thrown by Wait(Int32).

Applies to

WaitAsync()

Asynchronously waits to enter the SemaphoreSlim.

public:
 System::Threading::Tasks::Task ^ WaitAsync();
public System.Threading.Tasks.Task WaitAsync ();
member this.WaitAsync : unit -> System.Threading.Tasks.Task
Public Function WaitAsync () As Task

Returns

A task that will complete when the semaphore has been entered.

Exceptions

The SemaphoreSlim has been disposed.

Remarks

This method stores in the task it returns all non-usage exceptions that the method's synchronous counterpart can throw. If an exception is stored into the returned task, that exception will be thrown when the task is awaited. Usage exceptions, such as ArgumentException, are still thrown synchronously. For the stored exceptions, see the exceptions thrown by Wait().

Applies to

WaitAsync(Int32)

Asynchronously waits to enter the SemaphoreSlim, using a 32-bit signed integer to measure the time interval.

public:
 System::Threading::Tasks::Task<bool> ^ WaitAsync(int millisecondsTimeout);
public System.Threading.Tasks.Task<bool> WaitAsync (int millisecondsTimeout);
member this.WaitAsync : int -> System.Threading.Tasks.Task<bool>
Public Function WaitAsync (millisecondsTimeout As Integer) As Task(Of Boolean)

Parameters

millisecondsTimeout
Int32

The number of milliseconds to wait, Infinite (-1) to wait indefinitely, or zero to test the state of the wait handle and return immediately.

Returns

A task that will complete with a result of true if the current thread successfully entered the SemaphoreSlim, otherwise with a result of false.

Exceptions

The current instance has already been disposed.

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

-or-

millisecondsTimeout is greater than Int32.MaxValue.

Remarks

If the timeout is set to -1 milliseconds, the method waits indefinitely.

If the timeout is set to zero milliseconds, the method doesn't block. It tests the state of the wait handle and returns immediately.

This method stores in the task it returns all non-usage exceptions that the method's synchronous counterpart can throw. If an exception is stored into the returned task, that exception will be thrown when the task is awaited. Usage exceptions, such as ArgumentException, are still thrown synchronously. For the stored exceptions, see the exceptions thrown by Wait(Int32).

Applies to

WaitAsync(CancellationToken)

Asynchronously waits to enter the SemaphoreSlim, while observing a CancellationToken.

public:
 System::Threading::Tasks::Task ^ WaitAsync(System::Threading::CancellationToken cancellationToken);
public System.Threading.Tasks.Task WaitAsync (System.Threading.CancellationToken cancellationToken);
member this.WaitAsync : System.Threading.CancellationToken -> System.Threading.Tasks.Task
Public Function WaitAsync (cancellationToken As CancellationToken) As Task

Parameters

cancellationToken
CancellationToken

The CancellationToken token to observe.

Returns

A task that will complete when the semaphore has been entered.

Exceptions

The current instance has already been disposed.

cancellationToken was canceled.

Remarks

This method stores in the task it returns all non-usage exceptions that the method's synchronous counterpart can throw. If an exception is stored into the returned task, that exception will be thrown when the task is awaited. Usage exceptions, such as ArgumentException, are still thrown synchronously. For the stored exceptions, see the exceptions thrown by Wait().

Applies to

WaitAsync(TimeSpan)

Asynchronously waits to enter the SemaphoreSlim, using a TimeSpan to measure the time interval.

public:
 System::Threading::Tasks::Task<bool> ^ WaitAsync(TimeSpan timeout);
public System.Threading.Tasks.Task<bool> WaitAsync (TimeSpan timeout);
member this.WaitAsync : TimeSpan -> System.Threading.Tasks.Task<bool>
Public Function WaitAsync (timeout As TimeSpan) As Task(Of Boolean)

Parameters

timeout
TimeSpan

A TimeSpan that represents the number of milliseconds to wait, a TimeSpan that represents -1 milliseconds to wait indefinitely, or a TimeSpan that represents 0 milliseconds to test the wait handle and return immediately.

Returns

A task that will complete with a result of true if the current thread successfully entered the SemaphoreSlim, otherwise with a result of false.

Exceptions

The current instance has already been disposed.

timeout is a negative number other than -1, which represents an infinite timeout.

-or-

timeout is greater than Int32.MaxValue.

Remarks

If the timeout is set to -1 milliseconds, the method waits indefinitely.

If the timeout is set to zero milliseconds, the method doesn't block. It tests the state of the wait handle and returns immediately.

This method stores in the task it returns all non-usage exceptions that the method's synchronous counterpart can throw. If an exception is stored into the returned task, that exception will be thrown when the task is awaited. Usage exceptions, such as ArgumentException, are still thrown synchronously. For the stored exceptions, see the exceptions thrown by Wait(TimeSpan).

Applies to

WaitAsync(TimeSpan, CancellationToken)

Asynchronously waits to enter the SemaphoreSlim, using a TimeSpan to measure the time interval, while observing a CancellationToken.

public:
 System::Threading::Tasks::Task<bool> ^ WaitAsync(TimeSpan timeout, System::Threading::CancellationToken cancellationToken);
public System.Threading.Tasks.Task<bool> WaitAsync (TimeSpan timeout, System.Threading.CancellationToken cancellationToken);
member this.WaitAsync : TimeSpan * System.Threading.CancellationToken -> System.Threading.Tasks.Task<bool>
Public Function WaitAsync (timeout As TimeSpan, cancellationToken As CancellationToken) As Task(Of Boolean)

Parameters

timeout
TimeSpan

A TimeSpan that represents the number of milliseconds to wait, a TimeSpan that represents -1 milliseconds to wait indefinitely, or a TimeSpan that represents 0 milliseconds to test the wait handle and return immediately.

cancellationToken
CancellationToken

The CancellationToken token to observe.

Returns

A task that will complete with a result of true if the current thread successfully entered the SemaphoreSlim, otherwise with a result of false.

Exceptions

timeout is a negative number other than -1, which represents an infinite timeout.

-or-

timeout is greater than Int32.MaxValue.

cancellationToken was canceled.

The SemaphoreSlim has been disposed.

Remarks

If the timeout is set to -1 milliseconds, the method waits indefinitely.

If the timeout is set to zero milliseconds, the method doesn't block. It tests the state of the wait handle and returns immediately.

This method stores in the task it returns all non-usage exceptions that the method's synchronous counterpart can throw. If an exception is stored into the returned task, that exception will be thrown when the task is awaited. Usage exceptions, such as ArgumentException, are still thrown synchronously. For the stored exceptions, see the exceptions thrown by Wait(TimeSpan).

Applies to