SemaphoreSlim.Wait SemaphoreSlim.Wait SemaphoreSlim.Wait SemaphoreSlim.Wait Method

Definition

Blocks the current thread until it can enter the SemaphoreSlim.

Overloads

Wait(TimeSpan, CancellationToken) Wait(TimeSpan, CancellationToken) Wait(TimeSpan, CancellationToken)

Blocks the current thread until it can enter the SemaphoreSlim, using a TimeSpan that specifies the timeout, while observing a CancellationToken.

Wait(Int32, CancellationToken) Wait(Int32, CancellationToken) Wait(Int32, CancellationToken)

Blocks the current thread until it can enter the SemaphoreSlim, using a 32-bit signed integer that specifies the timeout, while observing a CancellationToken.

Wait(TimeSpan) Wait(TimeSpan) Wait(TimeSpan) Wait(TimeSpan)

Blocks the current thread until it can enter the SemaphoreSlim, using a TimeSpan to specify the timeout.

Wait(Int32) Wait(Int32) Wait(Int32) Wait(Int32)

Blocks the current thread until it can enter the SemaphoreSlim, using a 32-bit signed integer that specifies the timeout.

Wait() Wait() Wait() Wait()

Blocks the current thread until it can enter the SemaphoreSlim.

Wait(CancellationToken) Wait(CancellationToken) Wait(CancellationToken)

Blocks the current thread until it can enter the SemaphoreSlim, while observing a CancellationToken.

Wait(TimeSpan, CancellationToken) Wait(TimeSpan, CancellationToken) Wait(TimeSpan, CancellationToken)

Blocks the current thread until it can enter the SemaphoreSlim, using a TimeSpan that specifies the timeout, while observing a CancellationToken.

public:
 bool Wait(TimeSpan timeout, System::Threading::CancellationToken cancellationToken);
public bool Wait (TimeSpan timeout, System.Threading.CancellationToken cancellationToken);
member this.Wait : TimeSpan * System.Threading.CancellationToken -> bool

Parameters

timeout
TimeSpan TimeSpan TimeSpan 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

true if the current thread successfully entered the SemaphoreSlim; otherwise, false.

Exceptions

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

The semaphoreSlim instance has been disposed .-or-

The CancellationTokenSource that created cancellationToken has already 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.

If a thread or task is able to enter the semaphore, it decrements the CurrentCount property by one.

If cancellationToken is cancelled, or if a thread or task is blocked when calling Wait(TimeSpan, CancellationToken) and the timeout interval specified by millisecondsTimeout expires:

  • The thread or task doesn't enter the semaphore.
  • The CurrentCount property isn't decremented.

If cancellationToken is cancelled, the method throws an OperationCanceledException exception.

See also

Wait(Int32, CancellationToken) Wait(Int32, CancellationToken) Wait(Int32, CancellationToken)

Blocks the current thread until it can enter the SemaphoreSlim, using a 32-bit signed integer that specifies the timeout, while observing a CancellationToken.

public:
 bool Wait(int millisecondsTimeout, System::Threading::CancellationToken cancellationToken);
public bool Wait (int millisecondsTimeout, System.Threading.CancellationToken cancellationToken);
member this.Wait : int * System.Threading.CancellationToken -> bool

Parameters

millisecondsTimeout
Int32 Int32 Int32 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

true if the current thread successfully entered the SemaphoreSlim; otherwise, false.

Exceptions

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

The SemaphoreSlim instance has been disposed, or the CancellationTokenSource that created cancellationToken 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.

If a thread or task is able to enter the semaphore, it decrements the CurrentCount property by one.

If cancellationToken is cancelled, or if a thread or task is blocked when calling Wait(Int32, CancellationToken) and the timeout interval specified by millisecondsTimeout expires:

  • The thread or task doesn't enter the semaphore.
  • The CurrentCount property isn't decremented.

If cancellationToken is cancelled, the method throws an OperationCanceledException exception.

See also

Wait(TimeSpan) Wait(TimeSpan) Wait(TimeSpan) Wait(TimeSpan)

Blocks the current thread until it can enter the SemaphoreSlim, using a TimeSpan to specify the timeout.

public:
 bool Wait(TimeSpan timeout);
public bool Wait (TimeSpan timeout);
member this.Wait : TimeSpan -> bool
Public Function Wait (timeout As TimeSpan) As Boolean

Parameters

timeout
TimeSpan TimeSpan TimeSpan 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

true if the current thread successfully entered the SemaphoreSlim; otherwise, false.

Exceptions

timeout is a negative number other than -1, which represents an infinite timeout -or- timeout is greater than 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.

If a thread or task is able to enter the semaphore, it decrements the CurrentCount property by one.

If a thread or task is blocked when calling Wait(TimeSpan) and the timeout interval specified by millisecondsTimeout expires:

  • The thread or task doesn't enter the semaphore.
  • The CurrentCount property isn't decremented.
See also

Wait(Int32) Wait(Int32) Wait(Int32) Wait(Int32)

Blocks the current thread until it can enter the SemaphoreSlim, using a 32-bit signed integer that specifies the timeout.

public:
 bool Wait(int millisecondsTimeout);
public bool Wait (int millisecondsTimeout);
member this.Wait : int -> bool
Public Function Wait (millisecondsTimeout As Integer) As Boolean

Parameters

millisecondsTimeout
Int32 Int32 Int32 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

true if the current thread successfully entered the SemaphoreSlim; otherwise, false.

Exceptions

millisecondsTimeout is a negative number other than -1, which represents an infinite timeout -or- timeout is greater than 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.

If a thread or task is able to enter the semaphore, it decrements the CurrentCount property by one.

If a thread or task is blocked when calling Wait(Int32) and the timeout interval specified by millisecondsTimeout expires:

  • The thread or task doesn't enter the semaphore.
  • The CurrentCount property isn't decremented.
See also

Wait() Wait() Wait() Wait()

Blocks the current thread until it can enter the SemaphoreSlim.

public:
 void Wait();
public void Wait ();
member this.Wait : unit -> unit
Public Sub Wait ()

Exceptions

Remarks

If a thread or task is able to enter the semaphore, it decrements the CurrentCount property by one.

See also

Wait(CancellationToken) Wait(CancellationToken) Wait(CancellationToken)

Blocks the current thread until it can enter the SemaphoreSlim, while observing a CancellationToken.

public:
 void Wait(System::Threading::CancellationToken cancellationToken);
public void Wait (System.Threading.CancellationToken cancellationToken);
member this.Wait : System.Threading.CancellationToken -> unit

Parameters

Exceptions

The current instance has already been disposed.

-or-

The CancellationTokenSource that created cancellationToken has already been disposed.

Remarks

If a thread or task is able to enter the semaphore, it decrements the CurrentCount property by one.

If cancellationToken is cancelled, the thread or task doesn't enter the semaphore, and the CurrentCount property isn't decremented. Instead, the method throws an OperationCanceledException exception.

See also

Applies to