ListDictionary.IsSynchronized 属性

定义

获取一个值,该值指示 ListDictionary 是否是同步的(线程安全)。Gets a value indicating whether the ListDictionary is synchronized (thread safe).

public:
 property bool IsSynchronized { bool get(); };
public bool IsSynchronized { get; }
member this.IsSynchronized : bool
Public ReadOnly Property IsSynchronized As Boolean

属性值

Boolean

此属性始终返回 falseThis property always returns false.

实现

示例

下面的代码示例演示如何使用在 SyncRoot 整个枚举过程中锁定集合。The following code example shows how to lock the collection using the SyncRoot during the entire enumeration.

ListDictionary^ myCollection = gcnew ListDictionary();
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);
    }
}
ListDictionary myCollection = new ListDictionary();
lock(myCollection.SyncRoot)
{
    foreach (object item in myCollection)
    {
        // Insert your code here.
    }
}
Dim myCollection As New ListDictionary()
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.

注解

ListDictionary 实现 IsSynchronized 属性,因为它是接口所必需的 System.Collections.ICollectionListDictionary implements the IsSynchronized property because it is required by the System.Collections.ICollection interface.

派生类可以使用属性提供的同步版本 ListDictionary SyncRootDerived classes can provide a synchronized version of the ListDictionary 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.

适用于

另请参阅