Class ReaderSynchronized<TRead>
Represents the reader part of ReaderWriterSynchronized<TWrite, TRead>.
Inheritance
Namespace: InSync
Assembly: InSync.dll
Syntax
public class ReaderSynchronized<TRead> : object, ISynchronized<TRead>, IBareLock<TRead>, IBareLock where TRead : class
Type Parameters
Name | Description |
---|---|
TRead | The type of the reader to protect. |
Constructors
ReaderSynchronized(ReaderWriterLockSlim, TRead)
Initialize a ReaderSynchronized<TRead> with the specified readerWriterLockSlim and reader.
Declaration
public ReaderSynchronized(ReaderWriterLockSlim readerWriterLockSlim, TRead reader)
Parameters
Type | Name | Description |
---|---|---|
ReaderWriterLockSlim | readerWriterLockSlim | |
TRead | reader |
Fields
reader
The reader to protect.
Declaration
protected readonly TRead reader
Field Value
Type | Description |
---|---|
TRead |
readerWriterLockSlim
The
Declaration
protected readonly ReaderWriterLockSlim readerWriterLockSlim
Field Value
Type | Description |
---|---|
ReaderWriterLockSlim |
Methods
BarelyLock()
Synchronously acquires the lock and returns the protected non-null object.
Declaration
public TRead BarelyLock()
Returns
Type | Description |
---|---|
TRead | The protected non-null object. |
Exceptions
Type | Condition |
---|---|
LockException |
BarelyTryLock(out TRead)
Tries to acquire the lock. It returns true
and the protected non-null object if the lock is acquired.
Declaration
public bool BarelyTryLock(out TRead value)
Parameters
Type | Name | Description |
---|---|---|
TRead | value |
Returns
Type | Description |
---|---|
System.Boolean |
|
Exceptions
Type | Condition |
---|---|
LockException |
BarelyTryLock(Int32, out TRead)
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 TRead value)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | millisecondsTimeout | The number of milliseconds to wait, |
TRead | value |
Returns
Type | Description |
---|---|
System.Boolean |
|
Exceptions
Type | Condition |
---|---|
LockException |
BarelyTryLock(TimeSpan, out TRead)
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 TRead value)
Parameters
Type | Name | Description |
---|---|---|
TimeSpan | timeout | A |
TRead | value |
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<TRead> Lock()
Returns
Type | Description |
---|---|
GuardedValue<TRead> |
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<TRead> TryLock()
Returns
Type | Description |
---|---|
GuardedValue<TRead> | 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<TRead> TryLock(int millisecondsTimeout)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | millisecondsTimeout | The number of milliseconds to wait, |
Returns
Type | Description |
---|---|
GuardedValue<TRead> | 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<TRead> TryLock(TimeSpan timeout)
Parameters
Type | Name | Description |
---|---|---|
TimeSpan | timeout | A |
Returns
Type | Description |
---|---|
GuardedValue<TRead> | Null if the lock is not acquired. |
Exceptions
Type | Condition |
---|---|
LockException |
TryWithLock(Action<TRead>)
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<TRead> action)
Parameters
Type | Name | Description |
---|---|---|
Action<TRead> | action |
Returns
Type | Description |
---|---|
System.Boolean |
|
Exceptions
Type | Condition |
---|---|
LockException | |
UnlockException |
TryWithLock(Int32, Action<TRead>)
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<TRead> action)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | millisecondsTimeout | The number of milliseconds to wait, |
Action<TRead> | action |
Returns
Type | Description |
---|---|
System.Boolean |
|
Exceptions
Type | Condition |
---|---|
LockException | |
UnlockException |
TryWithLock(TimeSpan, Action<TRead>)
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<TRead> action)
Parameters
Type | Name | Description |
---|---|---|
TimeSpan | timeout | A |
Action<TRead> | action |
Returns
Type | Description |
---|---|
System.Boolean |
|
Exceptions
Type | Condition |
---|---|
LockException | |
UnlockException |
WithLock(Action<TRead>)
Locks, performs the action then unlocks. If the action throws an exception, the lock is released automatically.
Declaration
public void WithLock(Action<TRead> action)
Parameters
Type | Name | Description |
---|---|---|
Action<TRead> | action |
Exceptions
Type | Condition |
---|---|
LockException | |
UnlockException |
WithLock<TResult>(Func<TRead, 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<TRead, TResult> func)
Parameters
Type | Name | Description |
---|---|---|
Func<TRead, TResult> | func |
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 |