Class AsyncLock
Provides a lock an async method can wait for, held until the returned result is disposed: an instance's own, or a process-wide one by name.
public sealed class AsyncLock
- Inheritance
-
AsyncLock
- Inherited Members
Properties
IsLocked
Gets a value indicating whether the instance's lock is held.
public bool IsLocked { get; }
Property Value
Methods
LockAsync(string, CancellationToken)
Waits for the process-wide lock of a name.
public static Task<AsyncLock.ILockAsyncResult> LockAsync(string name, CancellationToken cancellationToken)
Parameters
namestringThe lock's name.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task<AsyncLock.ILockAsyncResult>
A task whose result holds the lock until it is disposed.
Exceptions
- OperationCanceledException
cancellationTokenwas canceled.- ArgumentNullException
nameis null.
LockAsync(string, TimeSpan, CancellationToken)
Waits for the process-wide lock of a name, at most a timeout. The name's lock lasts until no wait for it remains and every result taken for it, a timed-out one included, is disposed.
public static Task<AsyncLock.ILockAsyncResult> LockAsync(string name, TimeSpan timeout, CancellationToken cancellationToken)
Parameters
namestringThe lock's name.
timeoutTimeSpanHow long to wait, or InfiniteTimeSpan for no limit.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task<AsyncLock.ILockAsyncResult>
A task whose result tells whether the lock was taken, and holds it until it is disposed.
Exceptions
- OperationCanceledException
cancellationTokenwas canceled.- ArgumentOutOfRangeException
timeoutis negative, other than infinite, or too long.- ArgumentNullException
nameis null.
LockAsync(CancellationToken)
Waits for the instance's lock.
public Task<AsyncLock.ILockAsyncResult> LockAsync(CancellationToken cancellationToken)
Parameters
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task<AsyncLock.ILockAsyncResult>
A task whose result holds the lock until it is disposed.
Exceptions
- OperationCanceledException
cancellationTokenwas canceled.
LockAsync(TimeSpan, CancellationToken)
Waits for the instance's lock, at most a timeout.
public Task<AsyncLock.ILockAsyncResult> LockAsync(TimeSpan timeout, CancellationToken cancellationToken)
Parameters
timeoutTimeSpanHow long to wait, or InfiniteTimeSpan for no limit.
cancellationTokenCancellationTokenThe token to monitor for cancellation requests.
Returns
- Task<AsyncLock.ILockAsyncResult>
A task whose result tells whether the lock was taken, and holds it until it is disposed.
Exceptions
- OperationCanceledException
cancellationTokenwas canceled.- ArgumentOutOfRangeException
timeoutis negative, other than infinite, or too long.