NameObjectCollectionBase.KeysCollection.ICollection.IsSynchronized Propriedade

Definição

Obtém um valor que indica se o acesso à NameObjectCollectionBase.KeysCollection é sincronizado (thread-safe).

property bool System::Collections::ICollection::IsSynchronized { bool get(); };
bool System.Collections.ICollection.IsSynchronized { get; }
member this.System.Collections.ICollection.IsSynchronized : bool
 ReadOnly Property IsSynchronized As Boolean Implements ICollection.IsSynchronized

Valor da propriedade

true caso o acesso ao NameObjectCollectionBase.KeysCollection seja sincronizado (thread-safe); do contrário, false. O padrão é false.

Implementações

Exemplos

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

// Create a collection derived from NameObjectCollectionBase
NameObjectCollectionBase^ myBaseCollection = gcnew DerivedCollection();
// Get the ICollection from NameObjectCollectionBase.KeysCollection
ICollection^ myKeysCollection = myBaseCollection->Keys;
bool lockTaken = false;
try
{
    Monitor::Enter(myKeysCollection->SyncRoot, lockTaken);
    for each (Object^ item in myKeysCollection)
    {
        // Insert your code here.
    }
}
finally
{
    if (lockTaken)
    {
        Monitor::Exit(myKeysCollection->SyncRoot);
    }
}
// Create a collection derived from NameObjectCollectionBase
NameObjectCollectionBase myBaseCollection = new DerivedCollection();
// Get the ICollection from NameObjectCollectionBase.KeysCollection
ICollection myKeysCollection = myBaseCollection.Keys;
lock(myKeysCollection.SyncRoot)
{
    foreach (object item in myKeysCollection)
    {
        // Insert your code here.
    }
}
' Create a collection derived from NameObjectCollectionBase
Dim myBaseCollection As NameObjectCollectionBase = New DerivedCollection()
' Get the ICollection from NameObjectCollectionBase.KeysCollection
Dim myKeysCollection As ICollection = myBaseCollection.Keys
SyncLock myKeysCollection.SyncRoot
    For Each item As Object In myKeysCollection
        ' Insert your code here.
    Next item
End SyncLock

A recuperação do valor dessa propriedade é uma operação O(1).

Comentários

As classes derivadas podem fornecer sua própria versão sincronizada do NameObjectCollectionBase.KeysCollection usando a SyncRoot propriedade . O código de sincronização deve executar operações no SyncRoot do NameObjectCollectionBase.KeysCollection, não diretamente no NameObjectCollectionBase.KeysCollection. Isso garante a operação apropriada das coleções que são derivadas de outros objetos. Especificamente, ele mantém a sincronização adequada com outros threads que podem estar modificando simultaneamente o NameObjectCollectionBase.KeysCollection objeto.

A enumeração por meio de uma coleção não é um procedimento thread-safe intrínseco. Mesmo quando uma coleção está sincronizada, outros threads ainda podem modificar a coleção, o que faz o enumerador lançar uma exceção. Para garantir thread-safe durante a enumeração, é possível bloquear a coleção durante toda a enumeração ou verificar as exceções resultantes das alterações feitas por outros threads.

Aplica-se a

Confira também