Hashtable.IsSynchronized Hashtable.IsSynchronized Hashtable.IsSynchronized Hashtable.IsSynchronized Property

Definizione

Ottiene un valore che indica se l'accesso a Hashtable è sincronizzato (thread-safe).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

Valore della proprietà

true se l'accesso a Hashtable è sincronizzato (thread-safe); in caso contrario, false.true if access to the Hashtable is synchronized (thread safe); otherwise, false. Il valore predefinito è false.The default is false.

Implementazioni

Esempi

Nell'esempio seguente viene illustrato come sincronizzare un Hashtable, determinare se un Hashtable è sincronizzato e utilizzare una Hashtable sincronizzata.The following example shows how to synchronize a Hashtable, determine if a Hashtable is synchronized, and use a synchronized Hashtable.

#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.
 */
using System;
using System.Collections;
public class SamplesHashtable
{

   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. 

Commenti

Un Hashtable può supportare contemporaneamente un writer e più lettori.A Hashtable can support one writer and multiple readers concurrently. Per supportare più writer, è necessario che tutte le operazioni vengano eseguite tramite il wrapper restituito dal metodo Synchronized.To support multiple writers, all operations must be done through the wrapper returned by the Synchronized method.

L'enumerazione di una raccolta non è intrinsecamente una procedura thread-safe.Enumerating through a collection is intrinsically not a thread safe procedure. Anche se una raccolta è sincronizzata, è possibile che venga modificata da altri thread, con conseguente generazione di un'eccezione da parte dell'enumeratore.Even when a collection is synchronized, other threads can still modify the collection, which causes the enumerator to throw an exception. Per garantire la protezione dei thread durante l'enumerazione, è possibile bloccare la raccolta per l'intera enumerazione oppure intercettare le eccezioni determinate dalle modifiche apportate da altri thread.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.

Nell'esempio di codice seguente viene illustrato come bloccare la raccolta utilizzando SyncRoot durante l'intera enumerazione:The following code example shows how to lock the collection using the SyncRoot during the entire enumeration:

Hashtable^ myCollection = gcnew Hashtable();
bool lockTaken = false;

try
{
    Monitor::Enter(myCollection->SyncRoot, lockTaken);
    for each (Object^ item in myCollection)
    {
        // Insert your code here.
    }
}
finally
{
    if (lockTaken)
    {
        Monitor::Exit(myCollection->SyncRoot);
    }
}
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

Si applica a

Vedi anche