Hashtable.IsSynchronized-Eigenschaft
Ruft einen Wert ab, der angibt, ob der Zugriff auf die Hashtable synchronisiert (threadsicher) ist.
Namespace: System.Collections
Assembly: mscorlib (in mscorlib.dll)
Syntax
'Declaration
Public Overridable ReadOnly Property IsSynchronized As Boolean
'Usage
Dim instance As Hashtable
Dim value As Boolean
value = instance.IsSynchronized
public virtual bool IsSynchronized { get; }
public:
virtual property bool IsSynchronized {
bool get ();
}
/** @property */
public boolean get_IsSynchronized ()
public function get IsSynchronized () : boolean
Eigenschaftenwert
true, wenn der Zugriff auf die Hashtable synchronisiert (threadsicher) ist, andernfalls false. Der Standardwert ist false.
Hinweise
Eine Hashtable stellt eine gleichzeitige Unterstützung eines Writers und mehrerer Reader bereit. Wenn mehrere Writer unterstützt werden sollen, müssen alle Operationen über den Wrapper ausgeführt werden, der von der Synchronized-Methode zurückgegeben wird.
Die Enumeration einer Auflistung ist systemintern keine threadsichere Prozedur. Selbst wenn eine Auflistung synchronisiert wird, besteht die Möglichkeit, dass andere Threads sie ändern. Dies führt dazu, dass der Enumerator eine Ausnahme auslöst. Sie können während der Enumeration Threadsicherheit gewährleisten, indem Sie entweder die Auflistung während der gesamten Enumeration sperren oder die Ausnahmen abfangen, die durch Änderungen ausgelöst werden, die von anderen Threads vorgenommen werden.
Im folgenden Codebeispiel wird veranschaulicht, wie die Auflistung mithilfe von SyncRoot während der gesamten Enumeration gesperrt wird:
Hashtable myCollection = new Hashtable();
lock(myCollection.SyncRoot) {
foreach (Object item in myCollection) {
// Insert your code here.
}
}
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
Das Abrufen des Werts dieser Eigenschaft ist eine O(1)-Operation.
Beispiel
Im folgenden Beispiel wird gezeigt, wie Sie eine Hashtable synchronisieren, eine synchronisierte Hashtable verwenden und wie Sie ermitteln, ob eine Hashtable synchronisiert ist.
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.
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.
*/
#using <system.dll>
using namespace System;
using namespace System::Collections;
void main()
{
// Creates and initializes a new Hashtable.
Hashtable^ myHT = gcnew Hashtable;
myHT->Add( (int^)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 ? (String^)"synchronized" : "not synchronized" );
Console::WriteLine( "mySyncdHT is {0}.", mySyncdHT->IsSynchronized ? (String^)"synchronized" : "not synchronized" );
}
/*
This code produces the following output.
myHT is not synchronized.
mySyncdHT is synchronized.
*/
import System.*;
import System.Collections.*;
public class SamplesHashtable
{
public static void main(String[] args)
{
// Creates and initializes a new Hashtable.
Hashtable myHT = new Hashtable();
myHT.Add(new Integer(0), "zero");
myHT.Add(new Integer(1), "one");
myHT.Add(new Integer(2), "two");
myHT.Add(new Integer(3), "three");
myHT.Add(new Integer(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.get_IsSynchronized()) ?
"synchronized" : "not synchronized");
Console.WriteLine("mySyncdHT is {0}.",
(mySyncdHT.get_IsSynchronized()) ?
"synchronized" : "not synchronized");
} //main
} //SamplesHashtable
/*
This code produces the following output.
myHT is not synchronized.
mySyncdHT is synchronized.
*/
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.
Plattformen
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.
Versionsinformationen
.NET Framework
Unterstützt in: 2.0, 1.1, 1.0
.NET Compact Framework
Unterstützt in: 2.0, 1.0
Siehe auch
Referenz
Hashtable-Klasse
Hashtable-Member
System.Collections-Namespace
Synchronized
SyncRoot