Hashtable.IsSynchronized Propriedade

Definição

Obtém um valor que indica se o acesso ao Hashtable é sincronizado (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

Valor da propriedade

true se o Hashtable acesso ao for sincronizado (thread safe); caso contrário, false. O padrão é false.

Implementações

Exemplos

O exemplo a seguir mostra como sincronizar um Hashtable, determinar se um Hashtable é sincronizado e usar um sincronizado 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.

Comentários

É Hashtable possível dar suporte a um gravador e a vários leitores simultaneamente. Para dar suporte a vários gravadores, todas as operações devem ser feitas por meio do wrapper retornado pelo Synchronized método.

Enumerar por meio de uma coleção não é intrinsecamente um procedimento seguro de thread. Mesmo quando uma coleção é sincronizada, outros threads ainda podem modificar a coleção, o que faz com que o enumerador gere uma exceção. Para garantir a segurança do thread durante a enumeração, você pode bloquear a coleção durante toda a enumeração ou capturar as exceções resultantes de alterações feitas por outros threads.

O exemplo de código a seguir mostra como bloquear a coleção usando a SyncRoot enumeração durante toda a enumeração:

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

Aplica-se a

Confira também