SemaphoreSlimExtensions Class

Provides extension methods for SemaphoreSlim to simplify usage with disposable patterns.

public static class SemaphoreSlimExtensions

Nested types

NameDescription
SemaphoreSlimExtensions.SemaphoreDisposerRepresents a disposable struct that releases a semaphore when disposed. Every copy of the struct releases the semaphore, so dispose exactly one copy exactly once.

Methods

NameDescription
DisposableUnsafeWait(SemaphoreSlim, CancellationToken)Waits on the semaphore and returns a disposable struct that releases the semaphore when disposed. This method is unsafe because the struct can be copied, so each copy releases the semaphore again.
DisposableWait(SemaphoreSlim, CancellationToken)Waits on the semaphore and returns a disposable object that releases the semaphore when disposed. Disposing the object more than once, from any number of threads, releases the semaphore only once.
DisposableWaitAsync(SemaphoreSlim, CancellationToken)Asynchronously waits on the semaphore and returns a disposable object that releases the semaphore when disposed. Disposing the object more than once, from any number of threads, releases the semaphore only once.
DisposableWaitUnsafe(SemaphoreSlim, CancellationToken)Waits on the semaphore and returns a disposable struct that releases the semaphore when disposed. This method is unsafe because the struct can be copied, so each copy releases the semaphore again.
DisposableWaitUnsafeAsync(SemaphoreSlim, CancellationToken)Asynchronously waits on the semaphore and returns a disposable struct that releases the semaphore when disposed. This method is unsafe because the struct can be copied, so each copy releases the semaphore again.

DisposableUnsafeWait(SemaphoreSlim, CancellationToken)

SemaphoreSlimExtensions.cs
  • Obsolete: Use DisposableWaitUnsafe instead.

Waits on the semaphore and returns a disposable struct that releases the semaphore when disposed. This method is unsafe because the struct can be copied, so each copy releases the semaphore again.

[Obsolete("Use DisposableWaitUnsafe instead.")]
public static SemaphoreSlimExtensions.SemaphoreDisposer DisposableUnsafeWait(this SemaphoreSlim semaphore, CancellationToken cancellationToken = default)

Parameters

semaphore SemaphoreSlim
The semaphore to wait on.
cancellationToken CancellationToken
The cancellation token to observe.

Returns

SemaphoreSlimExtensions.SemaphoreDisposer A disposable struct that releases the semaphore when disposed.

DisposableWait(SemaphoreSlim, CancellationToken)

SemaphoreSlimExtensions.cs

Waits on the semaphore and returns a disposable object that releases the semaphore when disposed. Disposing the object more than once, from any number of threads, releases the semaphore only once.

public static IDisposable DisposableWait(this SemaphoreSlim semaphore, CancellationToken cancellationToken = default)

Parameters

semaphore SemaphoreSlim
The semaphore to wait on.
cancellationToken CancellationToken
The cancellation token to observe.

Returns

IDisposable A disposable object that releases the semaphore when disposed.

DisposableWaitAsync(SemaphoreSlim, CancellationToken)

SemaphoreSlimExtensions.cs

Asynchronously waits on the semaphore and returns a disposable object that releases the semaphore when disposed. Disposing the object more than once, from any number of threads, releases the semaphore only once.

net10.0

public static Task<IDisposable> DisposableWaitAsync(this SemaphoreSlim semaphore, CancellationToken cancellationToken = default)

net11.0

public static Task<IDisposable> DisposableWaitAsync(this SemaphoreSlim semaphore, CancellationToken cancellationToken = default)

Parameters

semaphore SemaphoreSlim
The semaphore to wait on.
cancellationToken CancellationToken
The cancellation token to observe.

Returns

Task<IDisposable> A task that returns a disposable object that releases the semaphore when disposed.

DisposableWaitUnsafe(SemaphoreSlim, CancellationToken)

SemaphoreSlimExtensions.cs

Waits on the semaphore and returns a disposable struct that releases the semaphore when disposed. This method is unsafe because the struct can be copied, so each copy releases the semaphore again.

public static SemaphoreSlimExtensions.SemaphoreDisposer DisposableWaitUnsafe(this SemaphoreSlim semaphore, CancellationToken cancellationToken = default)

Parameters

semaphore SemaphoreSlim
The semaphore to wait on.
cancellationToken CancellationToken
The cancellation token to observe.

Returns

SemaphoreSlimExtensions.SemaphoreDisposer A disposable struct that releases the semaphore when disposed.

DisposableWaitUnsafeAsync(SemaphoreSlim, CancellationToken)

SemaphoreSlimExtensions.cs

Asynchronously waits on the semaphore and returns a disposable struct that releases the semaphore when disposed. This method is unsafe because the struct can be copied, so each copy releases the semaphore again.

net10.0

public static Task<SemaphoreSlimExtensions.SemaphoreDisposer> DisposableWaitUnsafeAsync(this SemaphoreSlim semaphore, CancellationToken cancellationToken = default)

net11.0

public static Task<SemaphoreSlimExtensions.SemaphoreDisposer> DisposableWaitUnsafeAsync(this SemaphoreSlim semaphore, CancellationToken cancellationToken = default)

Parameters

semaphore SemaphoreSlim
The semaphore to wait on.
cancellationToken CancellationToken
The cancellation token to observe.

Returns

Task<SemaphoreSlimExtensions.SemaphoreDisposer> A task that returns a disposable struct that releases the semaphore when disposed.