Returns a synchronized (thread-safe) wrapper for the Hashtable.
- The Hashtable to synchronize.
A synchronized (thread-safe) wrapper for the Hashtable.
Type Reason ArgumentNullException table is null.
The Hashtable.Synchronized(Hashtable) method is thread safe for multiple readers and writers. Furthermore, the synchronized wrapper ensures that there is only one writer writing at a time.
Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads can still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.
The following code example shows how to lock the collection using the Hashtable.SyncRoot during the entire enumeration:
code reference: Classic Hashtable.IsSynchronized Example#2
This method is an O(1) operation.