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
initialCountintThe 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
timeoutintA timeout for waiting for the lock (in milliseconds).
cancellationTokenCancellationTokenA 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
cancellationTokenis canceled or thetimeoutexpires 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
cancellationTokenCancellationTokenA 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
cancellationTokenis signaled.
Exceptions
- OperationCanceledException
Thrown when
cancellationTokenis 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
timeoutTimeSpanA timeout for waiting for the lock.
cancellationTokenCancellationTokenA 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
cancellationTokenis signaled ortimeoutexpires.
Exceptions
- OperationCanceledException
Thrown when
cancellationTokenis canceled or thetimeoutexpires before semaphore access is granted.- ObjectDisposedException
Thrown when this semaphore is disposed before semaphore access is granted.