CollectionBase.ICollection.IsSynchronized Eigenschaft

Definition

Ruft einen Wert ab, der angibt, ob der Zugriff auf die CollectionBase synchronisiert (threadsicher) ist.

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

Eigenschaftswert

Boolean

true, wenn der Zugriff auf das CollectionBase synchronisiert (threadsicher) ist, andernfalls false. Der Standardwert ist false.

Implementiert

Hinweise

Eine CollectionBase Instanz wird nicht synchronisiert. Abgeleitete Klassen können eine synchronisierte Version der CollectionBase SyncRoot Eigenschaft bereitstellen.

Das Aufzählen durch eine Auflistung ist nicht ein sicheres Threadverfahren. Selbst wenn eine Auflistung synchronisiert wird, besteht die Möglichkeit, dass andere Threads sie ändern. Dies führt dazu, dass der Enumerator eine Ausnahme auslöst. Um während der Enumeration Threadsicherheit zu gewährleisten, können Sie entweder die Auflistung während der gesamten Enumeration sperren oder die Ausnahmen, die aus von anderen Threads stammenden Änderungen resultieren, abfangen.

Im folgenden Codebeispiel wird gezeigt, wie Sie die Auflistung mithilfe SyncRoot der gesamten Enumeration sperren:

// 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

Das Abrufen des Werts dieser Eigenschaft ist ein O(1) Vorgang.

Gilt für

Siehe auch