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
intThe 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
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
EnterAsync(int, CancellationToken)
Requests access to the lock.
public Task<AsyncSemaphore.Releaser> EnterAsync(int timeout, CancellationToken cancellationToken = default)
Parameters
timeout
intA timeout for waiting for the lock (in milliseconds).
cancellationToken
CancellationTokenA 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 thetimeout
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
CancellationTokenA 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
TimeSpanA timeout for waiting for the lock.
cancellationToken
CancellationTokenA 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 ortimeout
expires.
Exceptions
- OperationCanceledException
Thrown when
cancellationToken
is canceled or thetimeout
expires before semaphore access is granted.- ObjectDisposedException
Thrown when this semaphore is disposed before semaphore access is granted.