Method Lock
Lock(T, CancellationToken)
Acquires a lock for the specified key. If the lock is already held, it will wait until it can acquire the lock.
Declaration
public KeyLock<T> Lock(T key, CancellationToken cancellationToken = default)
Parameters
Type | Name | Description |
---|---|---|
T | key | The key to lock. |
CancellationToken | cancellationToken | Optional cancellation token. |
Returns
Type | Description |
---|---|
KeyLock<T> | The acquired lock. |
Lock(T, int, CancellationToken)
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 KeyLock<T> Lock(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 |
---|---|
KeyLock<T> | The acquired lock. |
Exceptions
Type | Condition |
---|---|
TimeoutException | Lock could not be acquired within the specified timeout. |
Lock(T, TimeSpan, CancellationToken)
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 KeyLock<T> Lock(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 |
---|---|
KeyLock<T> | The acquired lock. |
Exceptions
Type | Condition |
---|---|
TimeoutException | Lock could not be acquired within the specified timeout. |