Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Hashtable.IsSynchronized Property

Gets a value indicating whether access to the Hashtable is synchronized (thread-safe).

[Visual Basic]
Public Overridable ReadOnly Property IsSynchronized As Boolean  _
   Implements ICollection.IsSynchronized
[C#]
public virtual bool IsSynchronized {get;}
[C++]
public: __property virtual bool get_IsSynchronized();
[JScript]
public function get IsSynchronized() : Boolean;

Property Value

true if access to the Hashtable is synchronized (thread-safe); otherwise, false. The default is false.

Implements

ICollection.IsSynchronized

Remarks

A Hashtable can support one writer and multiple readers concurrently. To support multiple writers, all operations must be done through the wrapper returned by the Synchronized method.

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

Example

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. 

Requirements

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, Common Language Infrastructure (CLI) Standard

See Also

Hashtable Class | Hashtable Members | System.Collections Namespace | Synchronized | SyncRoot

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.