NameObjectCollectionBase.ICollection.IsSynchronized Proprietà

Definizione

Ottiene un valore che indica se l'accesso all'oggetto NameObjectCollectionBase è sincronizzato (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

Valore della proprietà

Boolean

true se l'accesso all'oggetto NameObjectCollectionBase è sincronizzato (thread-safe); in caso contrario, false. Il valore predefinito è false.

Implementazioni

Commenti

Un NameObjectCollectionBase oggetto non è sincronizzato. Le classi derivate possono fornire una versione sincronizzata di NameObjectCollectionBase utilizzando la SyncRoot proprietà .

L'enumerazione di una raccolta non è di per sé una procedura thread-safe. Anche se una raccolta è sincronizzata, è possibile che venga modificata da altri thread, con conseguente generazione di un'eccezione da parte dell'enumeratore. 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.

Nell'esempio di codice seguente viene illustrato come bloccare l'insieme usando la SyncRoot proprietà durante l'intera enumerazione.

// Create a collection derived from NameObjectCollectionBase
ICollection^ myCollection = gcnew DerivedCollection();
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);
    }
}
// Create a collection derived from NameObjectCollectionBase
ICollection myCollection = new DerivedCollection();
lock(myCollection.SyncRoot)
{
    foreach (object item in myCollection)
    {
        // Insert your code here.
    }
}
' Create a collection derived from NameObjectCollectionBase
Dim myCollection As ICollection = New DerivedCollection()
SyncLock myCollection.SyncRoot
    For Each item As Object In myCollection
        ' Insert your code here.
    Next item
End SyncLock

Il recupero del valore di questa proprietà è un'operazione O(1).

Si applica a

Vedi anche