CollectionBase.ICollection.IsSynchronized 屬性

定義

取得值,這個值表示對 CollectionBase 的存取是否同步 (安全執行緒)。

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

屬性值

如果 true 的存取為同步 (安全執行緒),則為 CollectionBase,否則為 false。 預設為 false

實作

備註

CollectionBase實例未同步處理。 衍生類別可以使用 屬性來提供 同步版本的 CollectionBaseSyncRoot

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

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

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

擷取此屬性的值是作業 O(1)

適用於

另請參閱