Class Synchronized<T>
Uses
Asynchronous operations are not supported. The protected object is non-null.
Inheritance
Namespace: InSync
Assembly: InSync.dll
Syntax
public class Synchronized<T> : object, ISynchronized<T>, IBareLock<T>, IBareLock where T : class
Type Parameters
| Name | Description |
|---|---|
| T | The type of the protected object. |
Constructors
Synchronized(Object, T)
Initializes a new Synchronized<T> with the specified lock and the object to protect.
Declaration
public Synchronized(object padLock, T value)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Object | padLock | The lock for synchronization |
| T | value | The object to protect. |
Fields
padLock
The lock for synchronization
Declaration
protected readonly object padLock
Field Value
| Type | Description |
|---|---|
| System.Object |
value
The non-null object to protect.
Declaration
protected readonly T value
Field Value
| Type | Description |
|---|---|
| T |
Methods
BarelyLock()
Synchronously acquires the lock and returns the protected non-null object.
Declaration
public T BarelyLock()
Returns
| Type | Description |
|---|---|
| T | The protected non-null object. |
Exceptions
| Type | Condition |
|---|---|
| LockException |
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, |
Returns
| Type | Description |
|---|---|
| System.Boolean |
|
Exceptions
| Type | Condition |
|---|---|
| LockException |
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, |
| T | value | The protected non-null object if the lock is acquired, otherwise, |
Returns
| Type | Description |
|---|---|
| System.Boolean |
|
Exceptions
| Type | Condition |
|---|---|
| LockException |
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 |
| T | value | The protected non-null object if the lock is acquired, otherwise, |
Returns
| Type | Description |
|---|---|
| System.Boolean |
|
Exceptions
| Type | Condition |
|---|---|
| LockException |
BarelyUnlock()
Releases the lock.
Declaration
public void BarelyUnlock()
Exceptions
| Type | Condition |
|---|---|
| UnlockException |
Lock()
Locks and returns a guard to allow access of the protected object and unlocking.
Declaration
public GuardedValue<T> Lock()
Returns
| Type | Description |
|---|---|
| GuardedValue<T> |
Exceptions
| Type | Condition |
|---|---|
| LockException |
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
| Type | Condition |
|---|---|
| LockException |
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, |
Returns
| Type | Description |
|---|---|
| GuardedValue<T> | Null if the lock is not acquired. |
Exceptions
| Type | Condition |
|---|---|
| LockException |
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 |
Returns
| Type | Description |
|---|---|
| GuardedValue<T> | Null if the lock is not acquired. |
Exceptions
| Type | Condition |
|---|---|
| LockException |
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 |
|
Exceptions
| Type | Condition |
|---|---|
| LockException | |
| UnlockException |
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, |
| Action<T> | action | The protected object is supplied as the argument of the action |
Returns
| Type | Description |
|---|---|
| System.Boolean |
|
Exceptions
| Type | Condition |
|---|---|
| LockException | |
| UnlockException |
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 |
| Action<T> | action | The protected object is supplied as the argument of the action |
Returns
| Type | Description |
|---|---|
| System.Boolean |
|
Exceptions
| Type | Condition |
|---|---|
| LockException | |
| UnlockException |
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
| Type | Condition |
|---|---|
| LockException | |
| UnlockException |
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
| Name | Description |
|---|---|
| TResult |
Exceptions
| Type | Condition |
|---|---|
| LockException | |
| UnlockException |
Explicit Interface Implementations
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, |
| System.Nullable<System.Object> | value | The protected non-null object if the lock is acquired, otherwise, |
Returns
| Type | Description |
|---|---|
| System.Boolean |
|
Exceptions
| Type | Condition |
|---|---|
| LockException |
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, |
Returns
| Type | Description |
|---|---|
| System.Boolean |
|
Exceptions
| Type | Condition |
|---|---|
| LockException |
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 |
| System.Nullable<System.Object> | value | The protected non-null object if the lock is acquired, otherwise, |
Returns
| Type | Description |
|---|---|
| System.Boolean |
|
Exceptions
| Type | Condition |
|---|---|
| LockException |