NameObjectCollectionBase.ICollection.IsSynchronized 屬性

定義

取得值,表示是否要同步處理 (執行緒安全) 對 NameObjectCollectionBase 物件的存取。

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

屬性值

如果要同步處理 (執行緒安全) 對 NameObjectCollectionBase 物件的存取,則為 true,否則為 false。 預設為 false

實作

備註

NameObjectCollectionBase物件未同步處理。 衍生類別可以使用 屬性來提供 同步版本的 NameObjectCollectionBaseSyncRoot

透過集合進行列舉在本質上並非安全執行緒程序。 即使集合經過同步化,其他的執行緒仍可修改該集合,使列舉值擲回例外狀況。 若要保證列舉過程的執行緒安全,您可以在整個列舉過程中鎖定集合,或攔截由其他執行緒的變更所造成的例外狀況。

下列程式代碼範例示範如何在整個列舉期間使用 SyncRoot 屬性來鎖定集合。

// 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) 運算。

適用於

另請參閱