NameObjectCollectionBase.ICollection.IsSynchronized 屬性

定義

取得值,表示是否要同步處理 (執行緒安全) 對 NameObjectCollectionBase 物件的存取。Gets a value indicating whether access to the NameObjectCollectionBase object is synchronized (thread safe).

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

屬性值

如果要同步處理 (執行緒安全) 對 NameObjectCollectionBase 物件的存取,則為 true,否則為 falsetrue if access to the NameObjectCollectionBase object is synchronized (thread safe); otherwise, false. 預設為 falseThe default is false.

實作

備註

NameObjectCollectionBase物件未同步處理。A NameObjectCollectionBase object is not synchronized. 衍生類別可以NameObjectCollectionBase SyncRoot使用屬性來提供的同步處理版本。Derived classes can provide a synchronized version of the NameObjectCollectionBase using the SyncRoot property.

透過集合進行列舉在本質上並非安全執行緒程序。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 property 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.

適用於

另請參閱