• Performance
  • API Documentation
  • Source Code
Show / Hide Table of Contents
  • InSync
    • AsyncSynchronized
    • AsyncSynchronized<T>
    • GuardedValue<T>
    • IAsyncSynchronized<T>
    • IBareAsyncLock
    • IBareAsyncLock<T>
    • IBareLock
    • IBareLock<T>
    • ISynchronized<T>
    • LockException
    • MultiSync
    • ReaderSynchronized<TRead>
    • ReaderWriterSynchronized<T>
    • ReaderWriterSynchronized<TWrite, TRead>
    • ReaderWriterSynchronizedFactory
    • Synchronized
    • Synchronized<T>
    • TimingMethod
    • UnlockException
    • UpgradeableReaderSynchronized<TRead>
    • ValueContainer<T>

Class ReaderWriterSynchronized<TWrite, TRead>

Uses to synchronize and only expose its protected reader or writer after a respective read or write synchronization begins. All the methods implementing its interfaces refer to the writer part. The readers are accessible via Reader and UpgradeableReader.

Asynchronous operations are not supported. The protected reader and writer are non-null.

Inheritance
System.Object
ReaderWriterSynchronized<TWrite, TRead>
ReaderWriterSynchronized<T>
Implements
ISynchronized<TWrite>
IBareLock<TWrite>
IBareLock
Namespace: InSync
Assembly: InSync.dll
Syntax
public class ReaderWriterSynchronized<TWrite, TRead> : object, ISynchronized<TWrite>, IBareLock<TWrite>, IBareLock where TWrite : class where TRead : class
Type Parameters
Name Description
TWrite

The type of the writer to protect.

TRead

The type of the reader to protect.

Constructors

ReaderWriterSynchronized(ReaderWriterLockSlim, TWrite, TRead)

Initializes a ReaderWriterSynchronized<TWrite, TRead> with the specified , writer to protect and reader to protect.

Declaration
public ReaderWriterSynchronized(ReaderWriterLockSlim readerWriterLockSlim, TWrite writer, TRead reader)
Parameters
Type Name Description
ReaderWriterLockSlim readerWriterLockSlim

The for synchronization.

TWrite writer

The writer to protect.

TRead reader

The reader to protect.

Fields

reader

The reader to protect.

Declaration
protected readonly TRead reader
Field Value
Type Description
TRead

readerWriterLockSlim

The for synchronization.

Declaration
protected readonly ReaderWriterLockSlim readerWriterLockSlim
Field Value
Type Description
ReaderWriterLockSlim

writer

The writer to protect.

Declaration
protected readonly TWrite writer
Field Value
Type Description
TWrite

Properties

Reader

Gets the synchronized reader.

Declaration
public ReaderSynchronized<TRead> Reader { get; }
Property Value
Type Description
ReaderSynchronized<TRead>

UpgradeableReader

Gets the synchronized upgradeable reader.

Declaration
public UpgradeableReaderSynchronized<TRead> UpgradeableReader { get; }
Property Value
Type Description
UpgradeableReaderSynchronized<TRead>

Methods

BarelyLock()

Synchronously acquires the lock and returns the protected non-null object.

Declaration
public TWrite BarelyLock()
Returns
Type Description
TWrite

The protected non-null object.

Exceptions
Type Condition
LockException

BarelyTryLock(out TWrite)

Tries to acquire the lock. It returns true and the protected non-null object if the lock is acquired.

Declaration
public bool BarelyTryLock(out TWrite value)
Parameters
Type Name Description
TWrite value
Returns
Type Description
System.Boolean

true if the lock is acquired.

Exceptions
Type Condition
LockException

BarelyTryLock(Int32, out TWrite)

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 TWrite 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.

TWrite value
Returns
Type Description
System.Boolean

true if the lock is acquired.

Exceptions
Type Condition
LockException

BarelyTryLock(TimeSpan, out TWrite)

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 TWrite 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.

TWrite value
Returns
Type Description
System.Boolean

true if the lock is acquired.

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<TWrite> Lock()
Returns
Type Description
GuardedValue<TWrite>
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<TWrite> TryLock()
Returns
Type Description
GuardedValue<TWrite>

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<TWrite> 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<TWrite>

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<TWrite> 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<TWrite>

Null if the lock is not acquired.

Exceptions
Type Condition
LockException

TryWithLock(Action<TWrite>)

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<TWrite> action)
Parameters
Type Name Description
Action<TWrite> action
Returns
Type Description
System.Boolean

true the action is performed.

Exceptions
Type Condition
LockException
UnlockException

TryWithLock(Int32, Action<TWrite>)

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<TWrite> 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<TWrite> action
Returns
Type Description
System.Boolean

true the action is performed.

Exceptions
Type Condition
LockException
UnlockException

TryWithLock(TimeSpan, Action<TWrite>)

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<TWrite> 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<TWrite> action
Returns
Type Description
System.Boolean

true the action is performed.

Exceptions
Type Condition
LockException
UnlockException

WithLock(Action<TWrite>)

Locks, performs the action then unlocks. If the action throws an exception, the lock is released automatically.

Declaration
public void WithLock(Action<TWrite> action)
Parameters
Type Name Description
Action<TWrite> action
Exceptions
Type Condition
LockException
UnlockException

WithLock<TResult>(Func<TWrite, 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<TWrite, TResult> func)
Parameters
Type Name Description
Func<TWrite, 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>)

Declaration
bool IBareLock.BarelyTryLock(int millisecondsTimeout, out object? value)
Parameters
Type Name Description
System.Int32 millisecondsTimeout
System.Nullable<System.Object> value
Returns
Type Description
System.Boolean

IBareLock.BarelyTryLock(out Nullable<Object>)

Declaration
bool IBareLock.BarelyTryLock(out object? value)
Parameters
Type Name Description
System.Nullable<System.Object> value
Returns
Type Description
System.Boolean

IBareLock.BarelyTryLock(TimeSpan, out Nullable<Object>)

Declaration
bool IBareLock.BarelyTryLock(TimeSpan timeout, out object? value)
Parameters
Type Name Description
TimeSpan timeout
System.Nullable<System.Object> value
Returns
Type Description
System.Boolean

Implements

ISynchronized<T>
IBareLock<T>
IBareLock
In This Article
Back to top Generated by DocFX