Class AsyncLock
An asynchronous mutual-exclusion lock that can be awaited (unlike lock, it can be held across an await).
It is not re-entrant: acquiring it twice from the same flow without releasing it deadlocks.
public class AsyncLock
- Inheritance
-
AsyncLock
- Inherited Members
Examples
private readonly AsyncLock _lock = new();
public async Task UpdateAsync()
{
using (await _lock.LockAsync().ConfigureAwait(false))
{
// Only one caller at a time executes this block.
await Task.Delay(10).ConfigureAwait(false);
}
}
Constructors
AsyncLock()
Initializes a new, unlocked instance of the AsyncLock class.
public AsyncLock()
Methods
LockAsync()
Asynchronously acquires the lock.
public Task<AsyncLock.Releaser> LockAsync()
Returns
- Task<AsyncLock.Releaser>
A task that completes when the lock is acquired. Dispose the resulting AsyncLock.Releaser to release the lock.