Method LockAsync
LockAsync(T, CancellationToken)
Asynchronously acquires a lock for the specified key. If the lock is already held, it will wait until it can acquire the lock.
Declaration
public ValueTask<KeyLock<T>> LockAsync(T key, CancellationToken cancellationToken = default)
Parameters
Type | Name | Description |
---|---|---|
T | key | The key to lock. |
CancellationToken | cancellationToken | Optional cancellation token. |
Returns
Type | Description |
---|---|
ValueTask<KeyLock<T>> | A task that contains the acquired lock when it completes. |
LockAsync(T, int, CancellationToken)
Asynchronously acquires a lock for the specified key. If the lock is already held, it will wait until it can acquire the lock or the specified timeout expires.
Declaration
public ValueTask<KeyLock<T>> LockAsync(T key, int millisecondsTimeout, CancellationToken cancellationToken = default)
Parameters
Type | Name | Description |
---|---|---|
T | key | The key to lock. |
int | millisecondsTimeout | The number of milliseconds to wait, or Infinite ( |
CancellationToken | cancellationToken | Optional cancellation token. |
Returns
Type | Description |
---|---|
ValueTask<KeyLock<T>> | A task that contains the acquired lock when it completes. |
Exceptions
Type | Condition |
---|---|
TimeoutException | Lock could not be acquired within the specified timeout. |
LockAsync(T, TimeSpan, CancellationToken)
Asynchronously acquires a lock for the specified key. If the lock is already held, it will wait until it can acquire the lock or the specified timeout expires.
Declaration
public ValueTask<KeyLock<T>> LockAsync(T key, TimeSpan timeout, CancellationToken cancellationToken = default)
Parameters
Type | Name | Description |
---|---|---|
T | key | The key to lock. |
TimeSpan | timeout | The amount of time to wait, or InfiniteTimeSpan to wait indefinitely. |
CancellationToken | cancellationToken | Optional cancellation token. |
Returns
Type | Description |
---|---|
ValueTask<KeyLock<T>> | A task that contains the acquired lock when it completes. |
Exceptions
Type | Condition |
---|---|
TimeoutException | Lock could not be acquired within the specified timeout. |