NameObjectCollectionBase.ICollection.SyncRoot NameObjectCollectionBase.ICollection.SyncRoot NameObjectCollectionBase.ICollection.SyncRoot Property

定義

取得可用來同步處理對 NameObjectCollectionBase 物件之存取的物件。Gets an object that can be used to synchronize access to the NameObjectCollectionBase object.

property System::Object ^ System::Collections::ICollection::SyncRoot { System::Object ^ get(); };
object System.Collections.ICollection.SyncRoot { get; }
 ReadOnly Property SyncRoot As Object Implements ICollection.SyncRoot

屬性值

可用來同步處理對 NameObjectCollectionBase 物件之存取的物件。An object that can be used to synchronize access to the NameObjectCollectionBase object.

實作

備註

在衍生的類別可以提供自己的同步處理的版本NameObjectCollectionBase類別使用SyncRoot屬性。Derived classes can provide their own synchronized version of the NameObjectCollectionBase class using the SyncRoot property. 同步處理的程式碼必須執行作業SyncRoot的屬性NameObjectCollectionBase物件,不要直接依賴NameObjectCollectionBase物件。The synchronizing code must perform operations on the SyncRoot property of the NameObjectCollectionBase object, not directly on the NameObjectCollectionBase object. 如此可確保衍生自其他物件的集合可以正常運作,This ensures proper operation of collections that are derived from other objects. 具體來說,它會維護適當的同步處理的其他執行緒可能同時修改NameObjectCollectionBase物件。Specifically, it maintains proper synchronization with other threads that might be simultaneously modifying the NameObjectCollectionBase object.

透過集合進行列舉在本質上並非安全執行緒程序。Enumerating through a collection is intrinsically not a thread-safe procedure. 即使集合經過同步化,其他的執行緒仍可修改該集合,使列舉值擲回例外狀況。Even when a collection is synchronized, other threads can still modify the collection, which causes the enumerator to throw an exception. 若要保證列舉過程的執行緒安全,您可以在整個列舉過程中鎖定集合,或攔截由其他執行緒的變更所造成的例外狀況。To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.

下列程式碼範例示範如何鎖定集合使用SyncRoot在整個列舉期間。The following code example shows how to lock the collection using the SyncRoot during the entire enumeration.

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

擷取這個屬性的值是一種 O(1) 運算。Retrieving the value of this property is an O(1) operation.

適用於

另請參閱