CollectionBase.ICollection.IsSynchronized Proprietà

Definizione

Ottiene un valore che indica se l'accesso a CollectionBase è 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à

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

Implementazioni

Commenti

Un'istanza CollectionBase di non è sincronizzata. Le classi derivate possono fornire una versione sincronizzata di CollectionBase utilizzando la SyncRoot proprietà .

L'enumerazione tramite una raccolta non è intrinsecamente 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 la raccolta usando durante SyncRoot l'intera enumerazione:

// Get the ICollection interface from the CollectionBase
// derived class.
ICollection^ myCollection = myCollectionBase;
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);
    }
}
// Get the ICollection interface from the CollectionBase
// derived class.
ICollection myCollection = myCollectionBase;
lock(myCollection.SyncRoot)
{
    foreach (object item in myCollection)
    {
        // Insert your code here.
    }
}
' Get the ICollection interface from the CollectionBase
' derived class.
Dim myCollection As ICollection = myCollectionBase
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