DictionaryBase.ICollection.IsSynchronized 屬性

定義

取得值,指出對 DictionaryBase 物件的存取是否為同步的 (執行緒安全)。Gets a value indicating whether access to a DictionaryBase object is synchronized (thread safe).

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

屬性值

Boolean

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

實作

範例

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

ICollection^ myCollection = gcnew ShortStringDictionary();
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);
    }
}
ICollection myCollection = new ShortStringDictionary();
lock(myCollection.SyncRoot)
{
    foreach (Object item in myCollection)
    {
        // Insert your code here.
    }
}
Dim myCollection As ICollection = New ShortStringDictionary()
SyncLock myCollection.SyncRoot
    For Each item In myCollection
        ' Insert your code here.
    Next item
End SyncLock

抓取這個屬性的值是一項作業 O(1)Retrieving the value of this property is an O(1) operation.

備註

DictionaryBase物件未同步處理。A DictionaryBase object is not synchronized. 衍生類別可以使用屬性提供類別的同步版本 DictionaryBase SyncRootDerived classes can provide a synchronized version of the DictionaryBase class 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.

適用於

另請參閱