SemaphoreSlim.Wait Method

Definition

Blocks the current thread until it can enter the SemaphoreSlim.

Overloads

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)

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)

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

Wait(Int32)

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

Wait()

Blocks the current thread until it can enter the SemaphoreSlim.

Wait(CancellationToken)

Blocks the current thread until it can enter the SemaphoreSlim, while observing a 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);
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public bool Wait (TimeSpan timeout, System.Threading.CancellationToken cancellationToken);
member this.Wait : TimeSpan * System.Threading.CancellationToken -> bool
[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
member this.Wait : TimeSpan * System.Threading.CancellationToken -> bool
Public Function Wait (timeout As TimeSpan, cancellationToken As CancellationToken) As 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 to observe.

Returns

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

Attributes

Exceptions

cancellationToken was canceled.

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

-or-.

timeout is greater than Int32.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

Applies to

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);
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public bool Wait (int millisecondsTimeout, System.Threading.CancellationToken cancellationToken);
member this.Wait : int * System.Threading.CancellationToken -> bool
[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
member this.Wait : int * System.Threading.CancellationToken -> bool
Public Function Wait (millisecondsTimeout As Integer, cancellationToken As CancellationToken) As 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

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

Attributes

Exceptions

cancellationToken was canceled.

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

-or-

millisecondsTimeout is greater than Int32.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

Applies to

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);
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public bool Wait (TimeSpan timeout);
member this.Wait : TimeSpan -> bool
[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
member this.Wait : TimeSpan -> bool
Public Function Wait (timeout As TimeSpan) As 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

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

Attributes

Exceptions

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

-or-

timeout is greater than Int32.MaxValue.

The semaphoreSlim instance 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 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

Applies to

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);
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public bool Wait (int millisecondsTimeout);
member this.Wait : int -> bool
[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
member this.Wait : int -> bool
Public Function Wait (millisecondsTimeout As Integer) As 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

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

Attributes

Exceptions

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

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.

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

Applies to

Wait()

Blocks the current thread until it can enter the SemaphoreSlim.

public:
 void Wait();
public void Wait ();
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public void Wait ();
member this.Wait : unit -> unit
[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
member this.Wait : unit -> unit
Public Sub Wait ()
Attributes

Exceptions

The current instance has already been disposed.

Remarks

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

See also

Applies to

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);
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public void Wait (System.Threading.CancellationToken cancellationToken);
member this.Wait : System.Threading.CancellationToken -> unit
[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
member this.Wait : System.Threading.CancellationToken -> unit
Public Sub Wait (cancellationToken As CancellationToken)

Parameters

cancellationToken
CancellationToken

The CancellationToken token to observe.

Attributes

Exceptions

cancellationToken was canceled.

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