Class KeyedMonitor<TKey>
Provides Monitor thread synchronization functionality over lock objects addressable by key(name).
This class is thread-safe. The internal implementation is based on a fixed-size array of Dictionary objects
to minimize inter-locking. Do not allocate/deallocate this class often, instead allocate
once per service that needs to synchronize by keys and call methods on the instance.
Inheritance
System.Object
KeyedMonitor<TKey>
Inherited Members
System.Object.Equals(System.Object)
System.Object.Equals(System.Object, System.Object)
System.Object.GetHashCode()
System.Object.GetType()
System.Object.MemberwiseClone()
System.Object.ReferenceEquals(System.Object, System.Object)
System.Object.ToString()
Namespace: NFX
Assembly: NFX.dll
Syntax
public sealed class KeyedMonitor<TKey>
Type Parameters
Constructors
KeyedMonitor(IEqualityComparer<TKey>)
Declaration
public KeyedMonitor(IEqualityComparer<TKey> comparer = null)
Parameters
Type |
Name |
Description |
System.Collections.Generic.IEqualityComparer<TKey> |
comparer |
|
Methods
Enter(TKey)
Performs Monitor.Enter() on TKey value.
Unlike TryEnter() this method does block
Declaration
public void Enter(TKey key)
Parameters
Type |
Name |
Description |
TKey |
key |
|
Exit(TKey)
Performs Monitor.Exit() on TKey value. Returns false in cases when lock was not taken which indicates an error
in the calling control flow
Declaration
public bool Exit(TKey key)
Parameters
Type |
Name |
Description |
TKey |
key |
|
Returns
Type |
Description |
System.Boolean |
|
Synchronized(TKey, Action)
Executes an action under a lock() taken on TKey value
Declaration
public void Synchronized(TKey key, Action action)
Parameters
Type |
Name |
Description |
TKey |
key |
|
System.Action |
action |
|
Synchronized<TResult>(TKey, Func<TResult>)
Executes a function under a lock() taken on TKey value
Declaration
public TResult Synchronized<TResult>(TKey key, Func<TResult> action)
Parameters
Type |
Name |
Description |
TKey |
key |
|
System.Func<TResult> |
action |
|
Returns
Type Parameters
TryEnter(TKey)
Tries to perform Monitor.TryEnter() on TKey value.
Returns true when lock was taken. Unlike Enter() this method does not block
Declaration
public bool TryEnter(TKey key)
Parameters
Type |
Name |
Description |
TKey |
key |
|
Returns
Type |
Description |
System.Boolean |
|
Extension Methods