Table of Contents

Class AsyncSemaphore

Namespace
Microsoft.VisualStudio.Threading
Assembly
Microsoft.VisualStudio.Threading.dll

An asynchronous SemaphoreSlim like class with more convenient release syntax.

public class AsyncSemaphore : IDisposable
Inheritance
AsyncSemaphore
Implements
Inherited Members

Remarks

This semaphore guarantees FIFO ordering.

This object does *not* need to be disposed of, as it does not hold unmanaged resources. Disposing this object has no effect on current users of the semaphore, and they are allowed to release their hold on the semaphore without exception. An ObjectDisposedException is thrown back at anyone asking to or waiting to enter the semaphore after Dispose() is called.

Constructors

AsyncSemaphore(int)

Initializes a new instance of the AsyncSemaphore class.

public AsyncSemaphore(int initialCount)

Parameters

initialCount int

The initial number of requests for the semaphore that can be granted concurrently.

Properties

CurrentCount

Gets the number of openings that remain in the semaphore.

public int CurrentCount { get; }

Property Value

int

Methods

Dispose()

Faults all pending semaphore waiters with ObjectDisposedException and rejects all subsequent attempts to enter the semaphore with the same exception.

public void Dispose()

Dispose(bool)

Disposes managed and unmanaged resources held by this instance.

protected virtual void Dispose(bool disposing)

Parameters

disposing bool

true if Dispose() was called; false if the object is being finalized.

EnterAsync(int, CancellationToken)

Requests access to the lock.

public Task<AsyncSemaphore.Releaser> EnterAsync(int timeout, CancellationToken cancellationToken = default)

Parameters

timeout int

A timeout for waiting for the lock (in milliseconds).

cancellationToken CancellationToken

A token whose cancellation signals lost interest in the lock.

Returns

Task<AsyncSemaphore.Releaser>

A task whose result is a releaser that should be disposed to release the lock.

Exceptions

OperationCanceledException

Thrown when cancellationToken is canceled or the timeout expires before semaphore access is granted.

ObjectDisposedException

Thrown when this semaphore is disposed before semaphore access is granted.

EnterAsync(CancellationToken)

Requests access to the lock.

public Task<AsyncSemaphore.Releaser> EnterAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

A token whose cancellation signals lost interest in the lock.

Returns

Task<AsyncSemaphore.Releaser>

A task whose result is a releaser that should be disposed to release the lock. This task may be canceled if cancellationToken is signaled.

Exceptions

OperationCanceledException

Thrown when cancellationToken is canceled before semaphore access is granted.

ObjectDisposedException

Thrown when this semaphore is disposed before semaphore access is granted.

EnterAsync(TimeSpan, CancellationToken)

Requests access to the lock.

public Task<AsyncSemaphore.Releaser> EnterAsync(TimeSpan timeout, CancellationToken cancellationToken = default)

Parameters

timeout TimeSpan

A timeout for waiting for the lock.

cancellationToken CancellationToken

A token whose cancellation signals lost interest in the lock.

Returns

Task<AsyncSemaphore.Releaser>

A task whose result is a releaser that should be disposed to release the lock. This task may be canceled if cancellationToken is signaled or timeout expires.

Exceptions

OperationCanceledException

Thrown when cancellationToken is canceled or the timeout expires before semaphore access is granted.

ObjectDisposedException

Thrown when this semaphore is disposed before semaphore access is granted.