Click or drag to resize
SafeDictionaryTKey, TValueSyncRoot Property

Namespace: CMS.Base
Assembly: CMS.Base (in CMS.Base.dll) Version: 9.0.0
Syntax
Remarks

Utilize this property when you need to perform multiple dictionary operations as a single atomic operation, or when you need to enumerate the entries.

Obtaining a lock does not prevent all other threads from read operations, since most read operations do not need to use locking.

Examples
Use the following snippet to enumerate the dictionary entries in a thread-safe way.
lock (dictionary.SyncRoot)
{
    foreach (var entry in dictionary)
    {
        // Perform some operation
        // Try to avoid time consuming operations to release the lock as soon as possible
        // No other thread can modify the dictionary when it is locked using the SyncRoot property
        // However, read access does not require locking, therefore any other thread can still read the dictionary's content
    }
}
See Also