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)

Source:
SemaphoreSlim.cs
Source:
SemaphoreSlim.cs
Source:
SemaphoreSlim.cs

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

C#
public bool Wait(TimeSpan timeout, System.Threading.CancellationToken cancellationToken);
C#
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public bool Wait(TimeSpan timeout, System.Threading.CancellationToken cancellationToken);

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

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Wait(Int32, CancellationToken)

Source:
SemaphoreSlim.cs
Source:
SemaphoreSlim.cs
Source:
SemaphoreSlim.cs

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

C#
public bool Wait(int millisecondsTimeout, System.Threading.CancellationToken cancellationToken);
C#
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public bool Wait(int millisecondsTimeout, System.Threading.CancellationToken cancellationToken);

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

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Wait(TimeSpan)

Source:
SemaphoreSlim.cs
Source:
SemaphoreSlim.cs
Source:
SemaphoreSlim.cs

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

C#
public bool Wait(TimeSpan timeout);
C#
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public bool Wait(TimeSpan timeout);

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

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Wait(Int32)

Source:
SemaphoreSlim.cs
Source:
SemaphoreSlim.cs
Source:
SemaphoreSlim.cs

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

C#
public bool Wait(int millisecondsTimeout);
C#
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public bool Wait(int millisecondsTimeout);

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

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Wait()

Source:
SemaphoreSlim.cs
Source:
SemaphoreSlim.cs
Source:
SemaphoreSlim.cs

Blocks the current thread until it can enter the SemaphoreSlim.

C#
public void Wait();
C#
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public void 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

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

Wait(CancellationToken)

Source:
SemaphoreSlim.cs
Source:
SemaphoreSlim.cs
Source:
SemaphoreSlim.cs

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

C#
public void Wait(System.Threading.CancellationToken cancellationToken);
C#
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public void Wait(System.Threading.CancellationToken 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

.NET 10 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0