Ask Learn
Preview
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign inThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Gets a value indicating whether access to the Hashtable is synchronized (thread safe).
public:
virtual property bool IsSynchronized { bool get(); };
public virtual bool IsSynchronized { get; }
member this.IsSynchronized : bool
Public Overridable ReadOnly Property IsSynchronized As Boolean
true
if access to the Hashtable is synchronized (thread safe); otherwise, false
. The default is false
.
The following example shows how to synchronize a Hashtable, determine if a Hashtable is synchronized, and use a synchronized Hashtable.
using System;
using System.Collections;
public class SamplesHashtable2
{
public static void Main()
{
// Creates and initializes a new Hashtable.
var 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.
*/
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, "synchronized", "not synchronized")
Console.WriteLine($"myHT is {msg}.")
msg = If(mySyncdHT.IsSynchronized, "synchronized", "not synchronized")
Console.WriteLine($"mySyncdHT is {msg}.")
End Sub
End Class
' This code produces the following output.
'
' myHT is not synchronized.
' mySyncdHT is synchronized.
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 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 SyncRoot during the entire enumeration:
var myCollection = new Hashtable();
lock (myCollection.SyncRoot)
{
foreach (object item in myCollection)
{
// Insert your code here.
}
}
Dim myCollection As New Hashtable()
SyncLock myCollection.SyncRoot
For Each item In myCollection
' Insert your code here.
Next
End SyncLock
Product | Versions |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 2.0, 2.1 |
UWP | 10.0 |
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Ask Learn is an AI assistant that can answer questions, clarify concepts, and define terms using trusted Microsoft documentation.
Please sign in to use Ask Learn.
Sign in