Returns a synchronized (thread-safe) wrapper for the Hashtable.
[Visual Basic] Public Shared Function Synchronized( _ ByVal table As Hashtable _ ) As Hashtable [C#] public static Hashtable Synchronized( Hashtable table ); [C++] public: static Hashtable* Synchronized( Hashtable* table ); [JScript] public static function Synchronized( table : Hashtable ) : Hashtable;
A synchronized (thread-safe) wrapper for the Hashtable.
| Exception Type | Condition |
|---|---|
| ArgumentNullException | table is a null reference (Nothing in Visual Basic). |
A Hashtable can safely support one writer and multiple readers concurrently. To support multiple writers, all operations must be done through this wrapper only.
Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads could 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.
[Visual Basic, C#] The following code example shows how to lock the collection using the SyncRoot during the entire enumeration:
[C#] Hashtable myCollection = new Hashtable(); lock( myCollection.SyncRoot ) { foreach ( Object item in myCollection ) { // Insert your code here. } } [Visual Basic] Dim myCollection As New Hashtable() Dim item As Object SyncLock myCollection.SyncRoot For Each item In myCollection ' Insert your code here. Next item End SyncLock
The following example shows how to synchronize a Hashtable, determine if a Hashtable is synchronized and use a synchronized Hashtable.
[Visual Basic] Imports System Imports System.Collections Public Class SamplesHashtable Public Shared Sub Main() ' Creates and initializes a new Hashtable. Dim myHT As New Hashtable() myHT.Add(0, "zero") myHT.Add(1, "one") myHT.Add(2, "two") myHT.Add(3, "three") myHT.Add(4, "four") ' Creates a synchronized wrapper around the Hashtable. Dim mySyncdHT As Hashtable = Hashtable.Synchronized(myHT) ' Displays the sychronization status of both Hashtables. Dim msg As String If myHT.IsSynchronized Then msg = "synchronized" Else msg = "not synchronized" End If Console.WriteLine("myHT is {0}.", msg) If mySyncdHT.IsSynchronized Then msg = "synchronized" Else msg = "not synchronized" End If Console.WriteLine("mySyncdHT is {0}.", msg) End Sub End Class ' This code produces the following output. ' ' myHT is not synchronized. ' mySyncdHT is synchronized. [C#] using System; using System.Collections; public class SamplesHashtable { public static void Main() { // Creates and initializes a new Hashtable. Hashtable myHT = new Hashtable(); myHT.Add( 0, "zero" ); myHT.Add( 1, "one" ); myHT.Add( 2, "two" ); myHT.Add( 3, "three" ); myHT.Add( 4, "four" ); // Creates a synchronized wrapper around the Hashtable. Hashtable mySyncdHT = Hashtable.Synchronized( myHT ); // Displays the sychronization status of both Hashtables. Console.WriteLine( "myHT is {0}.", myHT.IsSynchronized ? "synchronized" : "not synchronized" ); Console.WriteLine( "mySyncdHT is {0}.", mySyncdHT.IsSynchronized ? "synchronized" : "not synchronized" ); } } /* This code produces the following output. myHT is not synchronized. mySyncdHT is synchronized. */ [C++] #using <mscorlib.dll> #using <system.dll> using namespace System; using namespace System::Collections; void main() { // Creates and initializes a new Hashtable. Hashtable* myHT = new Hashtable(); myHT->Add( __box(0), S"zero" ); myHT->Add( __box(1), S"one" ); myHT->Add( __box(2), S"two" ); myHT->Add( __box(3), S"three" ); myHT->Add( __box(4), S"four" ); // Creates a synchronized wrapper around the Hashtable. Hashtable* mySyncdHT = Hashtable::Synchronized( myHT ); // Displays the sychronization status of both Hashtables. Console::WriteLine( S"myHT is {0}.", myHT->IsSynchronized ? S"synchronized" : S"not synchronized" ); Console::WriteLine( S"mySyncdHT is {0}.", mySyncdHT->IsSynchronized ? S"synchronized" : S"not synchronized" ); } /* This code produces the following output. myHT is not synchronized. mySyncdHT is synchronized. */ [JScript] import System import System.Collections // Creates and initializes a new Hashtable. var myHT : Hashtable = new Hashtable() myHT.Add(0, "zero") myHT.Add(1, "one") myHT.Add(2, "two") myHT.Add(3, "three") myHT.Add(4, "four") // Creates a synchronized wrapper around the Hashtable. var mySyncdHT : Hashtable = Hashtable.Synchronized(myHT) // Displays the sychronization status of both Hashtables. var msg : String if(myHT.IsSynchronized) msg = "synchronized" else msg = "not synchronized" Console.WriteLine("myHT is {0}.", msg) if(mySyncdHT.IsSynchronized) msg = "synchronized" else msg = "not synchronized" Console.WriteLine("mySyncdHT is {0}.", msg) // This code produces the following output. // // myHT is not synchronized. // mySyncdHT is synchronized.
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard
Hashtable Class | Hashtable Members | System.Collections Namespace | IsSynchronized | SyncRoot