Table of Contents

Class AsyncLock

Namespace
VpnHood.Net.Toolkit.Utils
Assembly
VpnHood.Net.Toolkit.dll

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

bool

Methods

LockAsync(string, CancellationToken)

Waits for the process-wide lock of a name.

public static Task<AsyncLock.ILockAsyncResult> LockAsync(string name, CancellationToken cancellationToken)

Parameters

name string

The lock's name.

cancellationToken CancellationToken

The token to monitor for cancellation requests.

Returns

Task<AsyncLock.ILockAsyncResult>

A task whose result holds the lock until it is disposed.

Exceptions

OperationCanceledException

cancellationToken was canceled.

ArgumentNullException

name is 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

name string

The lock's name.

timeout TimeSpan

How long to wait, or InfiniteTimeSpan for no limit.

cancellationToken CancellationToken

The 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

cancellationToken was canceled.

ArgumentOutOfRangeException

timeout is negative, other than infinite, or too long.

ArgumentNullException

name is null.

LockAsync(CancellationToken)

Waits for the instance's lock.

public Task<AsyncLock.ILockAsyncResult> LockAsync(CancellationToken cancellationToken)

Parameters

cancellationToken CancellationToken

The token to monitor for cancellation requests.

Returns

Task<AsyncLock.ILockAsyncResult>

A task whose result holds the lock until it is disposed.

Exceptions

OperationCanceledException

cancellationToken was canceled.

LockAsync(TimeSpan, CancellationToken)

Waits for the instance's lock, at most a timeout.

public Task<AsyncLock.ILockAsyncResult> LockAsync(TimeSpan timeout, CancellationToken cancellationToken)

Parameters

timeout TimeSpan

How long to wait, or InfiniteTimeSpan for no limit.

cancellationToken CancellationToken

The 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

cancellationToken was canceled.

ArgumentOutOfRangeException

timeout is negative, other than infinite, or too long.