Class AsyncSynchronized<T>
Uses to synchronize and only expose its protected object after a synchronization begins.
Various asynchronous operations are supported. Reentrant is not supported. The protected object is non-null.
Inheritance
System.Object
AsyncSynchronized<T>
Assembly: InSync.dll
Syntax
public class AsyncSynchronized<T> : object, ISynchronized<T>, IAsyncSynchronized<T>, IBareLock<T>, IBareLock, IBareAsyncLock<T>, IBareAsyncLock where T : class
Type Parameters
Name |
Description |
T |
The type of the protected object.
|
Constructors
AsyncSynchronized(SemaphoreSlim, T)
Declaration
public AsyncSynchronized(SemaphoreSlim semaphore, T value)
Parameters
Type |
Name |
Description |
SemaphoreSlim |
semaphore |
The semaphore for synchronization.
|
T |
value |
The object to protect.
|
Fields
semaphore
The semaphore for synchronization.
Declaration
protected readonly SemaphoreSlim semaphore
Field Value
Type |
Description |
SemaphoreSlim |
|
value
The non-null object to protect.
Declaration
protected readonly T value
Field Value
Methods
BarelyLock()
Synchronously acquires the lock and returns the protected non-null object.
Declaration
Returns
Type |
Description |
T |
The protected non-null object.
|
Exceptions
BarelyLockAsync()
Asynchronously acquires the lock and returns the protected non-null value.
Declaration
public Task<T> BarelyLockAsync()
Returns
Type |
Description |
Task<T> |
The protected non-null value.
|
Exceptions
BarelyLockAsync(CancellationToken)
Asynchronously acquires the lock and returns the protected non-null value.
Declaration
public Task<T> BarelyLockAsync(CancellationToken cancellationToken)
Parameters
Type |
Name |
Description |
CancellationToken |
cancellationToken |
|
Returns
Type |
Description |
Task<T> |
The protected non-null value.
|
Exceptions
BarelyTryLock(out T)
Tries to acquire the lock. It returns true
and the protected non-null object if the lock is acquired.
Declaration
public bool BarelyTryLock(out T value)
Parameters
Type |
Name |
Description |
T |
value |
The protected non-null object if the lock is acquired, otherwise, null is returned.
|
Returns
Type |
Description |
System.Boolean |
true if the lock is acquired.
|
Exceptions
BarelyTryLock(Int32, out T)
Tries to acquire the lock. It returns true
and the protected non-null object if the lock is acquired.
Declaration
public bool BarelyTryLock(int millisecondsTimeout, out T value)
Parameters
Type |
Name |
Description |
System.Int32 |
millisecondsTimeout |
The number of milliseconds to wait, (-1) to wait indefinitely, or zero to test the state of the wait handle and return immediately.
|
T |
value |
The protected non-null object if the lock is acquired, otherwise, null is returned.
|
Returns
Type |
Description |
System.Boolean |
true if the lock is acquired.
|
Exceptions
BarelyTryLock(TimeSpan, out T)
Tries to acquire the lock. It returns true
and the protected non-null object if the lock is acquired.
Declaration
public bool BarelyTryLock(TimeSpan timeout, out T value)
Parameters
Type |
Name |
Description |
TimeSpan |
timeout |
A that represents the number of milliseconds to wait, a that represents -1 milliseconds to wait indefinitely, or a that represents 0 milliseconds to test the wait handle and return immediately.
|
T |
value |
The protected non-null object if the lock is acquired, otherwise, null is returned.
|
Returns
Type |
Description |
System.Boolean |
true if the lock is acquired.
|
Exceptions
BarelyUnlock()
Declaration
public void BarelyUnlock()
Exceptions
Lock()
Locks and returns a guard to allow access of the protected object and unlocking.
Declaration
public GuardedValue<T> Lock()
Returns
Exceptions
LockAsync()
Asynchronously locks and returns a guard to allow access of the protected object and unlocking.
Declaration
public Task<GuardedValue<T>> LockAsync()
Returns
Exceptions
LockAsync(CancellationToken)
Asynchronously locks and returns a guard to allow access of the protected object and unlocking.
Declaration
public Task<GuardedValue<T>> LockAsync(CancellationToken cancellationToken)
Parameters
Type |
Name |
Description |
CancellationToken |
cancellationToken |
|
Returns
Exceptions
TryLock()
Tries to lock and returns a guard to allow access of the protected object and unlocking. If the lock is not acquired, this method returns null immediately.
Declaration
public GuardedValue<T> TryLock()
Returns
Type |
Description |
GuardedValue<T> |
Null if the lock is not acquired.
|
Exceptions
TryLock(Int32)
Tries to lock and returns a guard to allow access of the protected object and unlocking. If the lock is not acquired, this method returns null immediately.
Declaration
public GuardedValue<T> TryLock(int millisecondsTimeout)
Parameters
Type |
Name |
Description |
System.Int32 |
millisecondsTimeout |
The number of milliseconds to wait, (-1) to wait indefinitely, or zero to test the state of the wait handle and return immediately.
|
Returns
Type |
Description |
GuardedValue<T> |
Null if the lock is not acquired.
|
Exceptions
TryLock(TimeSpan)
Tries to lock and returns a guard to allow access of the protected object and unlocking. If the lock is not acquired, this method returns null immediately.
Declaration
public GuardedValue<T> TryLock(TimeSpan timeout)
Parameters
Type |
Name |
Description |
TimeSpan |
timeout |
A that represents the number of milliseconds to wait, a that represents -1 milliseconds to wait indefinitely, or a that represents 0 milliseconds to test the wait handle and return immediately.
|
Returns
Type |
Description |
GuardedValue<T> |
Null if the lock is not acquired.
|
Exceptions
TryWithLock(Action<T>)
Tries to lock and performs the action then unlocks. If the lock is not acquired, this method returns immediately. If the action throws an exception, the lock is released automatically.
Declaration
public bool TryWithLock(Action<T> action)
Parameters
Type |
Name |
Description |
Action<T> |
action |
The protected object is supplied as the argument of the action
|
Returns
Type |
Description |
System.Boolean |
true the action is performed.
|
Exceptions
TryWithLock(Int32, Action<T>)
Tries to lock and performs the action then unlocks. If the lock is not acquired, this method returns immediately. If the action throws an exception, the lock is released automatically.
Declaration
public bool TryWithLock(int millisecondsTimeout, Action<T> action)
Parameters
Type |
Name |
Description |
System.Int32 |
millisecondsTimeout |
The number of milliseconds to wait, (-1) to wait indefinitely, or zero to test the state of the wait handle and return immediately.
|
Action<T> |
action |
The protected object is supplied as the argument of the action
|
Returns
Type |
Description |
System.Boolean |
true the action is performed.
|
Exceptions
TryWithLock(TimeSpan, Action<T>)
Tries to lock and performs the action then unlocks. If the lock is not acquired, this method returns immediately. If the action throws an exception, the lock is released automatically.
Declaration
public bool TryWithLock(TimeSpan timeout, Action<T> action)
Parameters
Type |
Name |
Description |
TimeSpan |
timeout |
A that represents the number of milliseconds to wait, a that represents -1 milliseconds to wait indefinitely, or a that represents 0 milliseconds to test the wait handle and return immediately.
|
Action<T> |
action |
The protected object is supplied as the argument of the action
|
Returns
Type |
Description |
System.Boolean |
true the action is performed.
|
Exceptions
WithLock(Action<T>)
Locks, performs the action then unlocks. If the action throws an exception, the lock is released automatically.
Declaration
public void WithLock(Action<T> action)
Parameters
Type |
Name |
Description |
Action<T> |
action |
The protected object is supplied as the argument of the action
|
Exceptions
WithLock<TResult>(Func<T, TResult>)
Locks, call the function then unlocks. The returned value from the function is returned. If the function throws an exception, the lock is released automatically.
Declaration
public TResult WithLock<TResult>(Func<T, TResult> func)
Parameters
Type |
Name |
Description |
Func<T, TResult> |
func |
The protected object is supplied as the argument of the function. The returned value is returned by this method.
|
Returns
Type |
Description |
TResult |
The returned value from the function.
|
Type Parameters
Exceptions
WithLockAsync(Action<T>)
Asynchronously locks, performs the action on the captured context or task scheduler (if any) then unlocks. If the action throws an exception, the lock is released automatically.
Declaration
public Task WithLockAsync(Action<T> action)
Parameters
Type |
Name |
Description |
Action<T> |
action |
The protected object is supplied as the argument of the action
|
Returns
Exceptions
WithLockAsync(Action<T>, CancellationToken)
Asynchronously locks, performs the action on the captured context or task scheduler (if any) then unlocks. If the function throws an exception, the lock is released automatically.
Declaration
public Task WithLockAsync(Action<T> action, CancellationToken cancellationToken)
Parameters
Type |
Name |
Description |
Action<T> |
action |
The protected object is supplied as the argument of the action
|
CancellationToken |
cancellationToken |
|
Returns
Exceptions
WithLockAsync(Action<T>, CancellationToken, Boolean)
Asynchronously locks, synchronously performs the action then unlocks. If the function throws an exception, the lock is released automatically.
Declaration
public Task WithLockAsync(Action<T> action, CancellationToken cancellationToken, bool onCapturedContext)
Parameters
Type |
Name |
Description |
Action<T> |
action |
The protected object is supplied as the argument of the action
|
CancellationToken |
cancellationToken |
|
System.Boolean |
onCapturedContext |
true to perform the action on the original context or task scheduler if there is a context.
|
Returns
Exceptions
WithLockAsync(Action<T>, Boolean)
Asynchronously locks, performs the action then unlocks. If the action throws an exception, the lock is released automatically.
Declaration
public Task WithLockAsync(Action<T> action, bool onCapturedContext)
Parameters
Type |
Name |
Description |
Action<T> |
action |
The protected object is supplied as the argument of the action
|
System.Boolean |
onCapturedContext |
true to perform the action on the original context or task scheduler if there is a context.
|
Returns
Exceptions
WithLockAsync(Func<T, Task>)
Asynchronously locks, performs the action on the captured context or task scheduler (if any) then unlocks. If the action throws an exception, the lock is released automatically.
Declaration
public Task WithLockAsync(Func<T, Task> asyncAction)
Parameters
Type |
Name |
Description |
Func<T, Task> |
asyncAction |
The protected object is supplied as the argument of the action
|
Returns
Exceptions
WithLockAsync(Func<T, Task>, CancellationToken)
Asynchronously locks, performs the action on the captured context or task scheduler (if any) then unlocks. If the action throws an exception, the lock is released automatically.
Declaration
public Task WithLockAsync(Func<T, Task> asyncAction, CancellationToken cancellationToken)
Parameters
Type |
Name |
Description |
Func<T, Task> |
asyncAction |
The protected object is supplied as the argument of the action
|
CancellationToken |
cancellationToken |
|
Returns
Exceptions
WithLockAsync(Func<T, Task>, CancellationToken, Boolean)
Asynchronously locks, performs the action then unlocks. If the action throws an exception, the lock is released automatically.
Declaration
public Task WithLockAsync(Func<T, Task> asyncAction, CancellationToken cancellationToken, bool onCapturedContext)
Parameters
Type |
Name |
Description |
Func<T, Task> |
asyncAction |
The protected object is supplied as the argument of the action
|
CancellationToken |
cancellationToken |
|
System.Boolean |
onCapturedContext |
true to perform the action on the original context or task scheduler if there is a context.
|
Returns
Exceptions
WithLockAsync(Func<T, Task>, Boolean)
Asynchronously locks, performs the action then unlocks. If the action throws an exception, the lock is released automatically.
Declaration
public Task WithLockAsync(Func<T, Task> asyncAction, bool onCapturedContext)
Parameters
Type |
Name |
Description |
Func<T, Task> |
asyncAction |
The protected object is supplied as the argument of the action
|
System.Boolean |
onCapturedContext |
true to perform the action on the original context or task scheduler if there is a context.
|
Returns
Exceptions
WithLockAsync<TResult>(Func<T, TResult>)
Asynchronously locks, calls the function on the captured context or task scheduler (if any) then unlocks. The returned value from the function is returned. If the function throws an exception, the lock is released automatically.
Declaration
public Task<TResult> WithLockAsync<TResult>(Func<T, TResult> func)
Parameters
Type |
Name |
Description |
Func<T, TResult> |
func |
The protected object is supplied as the argument of the function. The returned value is returned by this method.
|
Returns
Type |
Description |
Task<TResult> |
|
Type Parameters
Exceptions
WithLockAsync<TResult>(Func<T, TResult>, CancellationToken)
Asynchronously locks, calls the function on the captured context or task scheduler (if any) then unlocks. The returned value from the function is returned. If the function throws an exception, the lock is released automatically.
Declaration
public Task<TResult> WithLockAsync<TResult>(Func<T, TResult> func, CancellationToken cancellationToken)
Parameters
Type |
Name |
Description |
Func<T, TResult> |
func |
The protected object is supplied as the argument of the function. The returned value is returned by this method.
|
CancellationToken |
cancellationToken |
|
Returns
Type |
Description |
Task<TResult> |
|
Type Parameters
Exceptions
WithLockAsync<TResult>(Func<T, TResult>, CancellationToken, Boolean)
Asynchronously locks, synchronously calls the function then unlocks. The returned value from the function is returned. If the function throws an exception, the lock is released automatically.
Declaration
public Task<TResult> WithLockAsync<TResult>(Func<T, TResult> func, CancellationToken cancellationToken, bool onCapturedContext)
Parameters
Type |
Name |
Description |
Func<T, TResult> |
func |
The protected object is supplied as the argument of the function. The returned value is returned by this method.
|
CancellationToken |
cancellationToken |
|
System.Boolean |
onCapturedContext |
true to call the function on the original context or task scheduler if there is a context.
|
Returns
Type |
Description |
Task<TResult> |
|
Type Parameters
Exceptions
WithLockAsync<TResult>(Func<T, TResult>, Boolean)
Asynchronously locks, calls the function then unlocks. The returned value from the function is returned. If the function throws an exception, the lock is released automatically.
Declaration
public Task<TResult> WithLockAsync<TResult>(Func<T, TResult> func, bool onCapturedContext)
Parameters
Type |
Name |
Description |
Func<T, TResult> |
func |
The protected object is supplied as the argument of the function. The returned value is returned by this method.
|
System.Boolean |
onCapturedContext |
true to calls the function on the original context or task scheduler if there is a context.
|
Returns
Type |
Description |
Task<TResult> |
|
Type Parameters
Exceptions
WithLockAsync<TResult>(Func<T, Task<TResult>>)
Asynchronously locks, calls the function on the captured context or task scheduler (if any) then unlocks. The returned value from the function is returned. If the function throws an exception, the lock is released automatically.
Declaration
public Task<TResult> WithLockAsync<TResult>(Func<T, Task<TResult>> asyncFunc)
Parameters
Type |
Name |
Description |
Func<T, Task<TResult>> |
asyncFunc |
The protected object is supplied as the argument of the function. The returned value is returned by this method.
|
Returns
Type |
Description |
Task<TResult> |
|
Type Parameters
Exceptions
WithLockAsync<TResult>(Func<T, Task<TResult>>, CancellationToken)
Asynchronously locks, calls the function on the captured context or task scheduler (if any) then unlocks. The returned value from the function is returned. If the function throws an exception, the lock is released automatically.
Declaration
public Task<TResult> WithLockAsync<TResult>(Func<T, Task<TResult>> asyncFunc, CancellationToken cancellationToken)
Parameters
Type |
Name |
Description |
Func<T, Task<TResult>> |
asyncFunc |
The protected object is supplied as the argument of the function. The returned value is returned by this method.
|
CancellationToken |
cancellationToken |
|
Returns
Type |
Description |
Task<TResult> |
|
Type Parameters
Exceptions
WithLockAsync<TResult>(Func<T, Task<TResult>>, CancellationToken, Boolean)
Asynchronously locks, calls the function then unlocks. The returned value from the function is returned. If the function throws an exception, the lock is released automatically.
Declaration
public Task<TResult> WithLockAsync<TResult>(Func<T, Task<TResult>> asyncFunc, CancellationToken cancellationToken, bool onCapturedContext)
Parameters
Type |
Name |
Description |
Func<T, Task<TResult>> |
asyncFunc |
The protected object is supplied as the argument of the function. The returned value is returned by this method.
|
CancellationToken |
cancellationToken |
|
System.Boolean |
onCapturedContext |
true to call the function on the original context or task scheduler if there is a context.
|
Returns
Type |
Description |
Task<TResult> |
|
Type Parameters
Exceptions
WithLockAsync<TResult>(Func<T, Task<TResult>>, Boolean)
Asynchronously locks, calls the function then unlocks. The returned value from the function is returned. If the function throws an exception, the lock is released automatically.
Declaration
public Task<TResult> WithLockAsync<TResult>(Func<T, Task<TResult>> asyncFunc, bool onCapturedContext)
Parameters
Type |
Name |
Description |
Func<T, Task<TResult>> |
asyncFunc |
The protected object is supplied as the argument of the function. The returned value is returned by this method.
|
System.Boolean |
onCapturedContext |
true to call the function on the original context or task scheduler if there is a context.
|
Returns
Type |
Description |
Task<TResult> |
|
Type Parameters
Exceptions
Explicit Interface Implementations
IBareAsyncLock.BarelyLockAsync()
Declaration
Task<object> IBareAsyncLock.BarelyLockAsync()
Returns
Type |
Description |
Task<System.Object> |
|
IBareAsyncLock.BarelyLockAsync(CancellationToken)
Declaration
Task<object> IBareAsyncLock.BarelyLockAsync(CancellationToken cancellationToken)
Parameters
Type |
Name |
Description |
CancellationToken |
cancellationToken |
|
Returns
Type |
Description |
Task<System.Object> |
|
IBareAsyncLock.BarelyTryLock(out Nullable<Object>)
Tries to acquire the lock. It returns true
and the protected non-null object if the lock is acquired.
Declaration
bool IBareAsyncLock.BarelyTryLock(out object? value)
Parameters
Type |
Name |
Description |
System.Nullable<System.Object> |
value |
The protected non-null object if the lock is acquired, otherwise, null is returned.
|
Returns
Type |
Description |
System.Boolean |
true if the lock is acquired.
|
Exceptions
IBareLock.BarelyLock()
Declaration
object IBareLock.BarelyLock()
Returns
Type |
Description |
System.Object |
|
IBareLock.BarelyTryLock(Int32, out Nullable<Object>)
Tries to acquire the lock. It returns true
and the protected non-null object if the lock is acquired.
Declaration
bool IBareLock.BarelyTryLock(int millisecondsTimeout, out object? value)
Parameters
Type |
Name |
Description |
System.Int32 |
millisecondsTimeout |
The number of milliseconds to wait, (-1) to wait indefinitely, or zero to test the state of the wait handle and return immediately.
|
System.Nullable<System.Object> |
value |
The protected non-null object if the lock is acquired, otherwise, null is returned.
|
Returns
Type |
Description |
System.Boolean |
true if the lock is acquired.
|
Exceptions
IBareLock.BarelyTryLock(out Nullable<Object>)
Tries to acquire the lock. It returns true
and the protected non-null object if the lock is acquired.
Declaration
bool IBareLock.BarelyTryLock(out object? value)
Parameters
Type |
Name |
Description |
System.Nullable<System.Object> |
value |
The protected non-null object if the lock is acquired, otherwise, null is returned.
|
Returns
Type |
Description |
System.Boolean |
true if the lock is acquired.
|
Exceptions
IBareLock.BarelyTryLock(TimeSpan, out Nullable<Object>)
Tries to acquire the lock. It returns true
and the protected non-null object if the lock is acquired.
Declaration
bool IBareLock.BarelyTryLock(TimeSpan timeout, out object? value)
Parameters
Type |
Name |
Description |
TimeSpan |
timeout |
A that represents the number of milliseconds to wait, a that represents -1 milliseconds to wait indefinitely, or a that represents 0 milliseconds to test the wait handle and return immediately.
|
System.Nullable<System.Object> |
value |
The protected non-null object if the lock is acquired, otherwise, null is returned.
|
Returns
Type |
Description |
System.Boolean |
true if the lock is acquired.
|
Exceptions
Implements