Method TryEnterWriteGuardAsync
TryEnterWriteGuardAsync(TimeSpan, CancellationToken)
Asynchronously tries to enter the lock in write mode within a time limit and returns a guard whose IsEntered property says whether it did. Disposing the guard leaves the lock, or does nothing if the lock was not entered.
Declaration
public ValueTask<AsyncWriteGuard> TryEnterWriteGuardAsync(TimeSpan timeout, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| TimeSpan | timeout | The time to wait for the lock: zero to not wait, or InfiniteTimeSpan to wait without a limit. At most MaxValue milliseconds. A time that is not a whole number of milliseconds is rounded up to the next whole millisecond. |
| CancellationToken | cancellationToken | A token that cancels the wait. |
Returns
| Type | Description |
|---|---|
| ValueTask<AsyncWriteGuard> | A task that contains the guard. The task has already completed, and nothing is allocated, when the lock is entered without waiting or
|
Remarks
The call waits while the lock is held in either mode and while writers that arrived earlier are waiting. An exception that the time provider throws when it creates the timer of the time limit is in the returned task. The lock is not reentrant, so entering it again while the caller holds it can wait until the time limit passes, or forever without one; see AsyncReadWriteLockPreference.
Await the returned task once, and give the time limit to this call. A limit put on the task itself, such as with WaitAsync, ends the
await but not the wait, and a hold that the lock grants after it is never released.
Exceptions
| Type | Condition |
|---|---|
| ArgumentOutOfRangeException |
|
| OperationCanceledException |
|