ICollection.SyncRoot ICollection.SyncRoot ICollection.SyncRoot ICollection.SyncRoot Property

定义

获取可用于同步对 ICollection 的访问的对象。Gets an object that can be used to synchronize access to the ICollection.

public:
 property System::Object ^ SyncRoot { System::Object ^ get(); };
public object SyncRoot { get; }
member this.SyncRoot : obj
Public ReadOnly Property SyncRoot As Object

属性值

可用于同步对 ICollection 的访问的对象。An object that can be used to synchronize access to the ICollection.

注解

对于其基础存储区不公用的集合,预期的实现是返回当前实例。For collections whose underlying store is not publicly available, the expected implementation is to return the current instance. 请注意当前实例的指针可能足以满足自动换行; 的其他集合的集合这些集合应返回基础集合SyncRoot属性。Note that the pointer to the current instance might not be sufficient for collections that wrap other collections; those should return the underlying collection's SyncRoot property.

System.Collections 命名空间中的大多数集合类也实现 Synchronized 方法,该方法对基础集合提供同步的包装。Most collection classes in the System.Collections namespace also implement a Synchronized method, which provides a synchronized wrapper around the underlying collection. 不过,派生类可使用 SyncRoot 属性来提供各自的同步版本的集合。However, derived classes can provide their own synchronized version of the collection using the SyncRoot property. 同步代码必须对集合的 SyncRoot 属性执行操作,而不是直接对集合执行操作。The synchronizing code must perform operations on the SyncRoot property of the collection, not directly on the collection. 这样可确保对从其他对象派生的集合正确地执行操作。This ensures proper operation of collections that are derived from other objects. 具体地说,这样可与可能同时对集合实例进行修改的其他线程保持正确的同步。Specifically, it maintains proper synchronization with other threads that might be simultaneously modifying the collection instance.

如果没有Synchronized方法对集合的预期使用情况SyncRoot如下所示:In the absence of a Synchronized method on a collection, the expected usage for SyncRoot looks as follows:

ICollection^ myCollection = someCollection;
bool lockTaken = false;
try
{
    Monitor::Enter(myCollection->SyncRoot, lockTaken);
    // Some operation on the collection, which is now thread safe.
}
finally
{
    if (lockTaken)
    {
        Monitor::Exit(myCollection->SyncRoot);
    }
}
ICollection myCollection = someCollection;
lock(myCollection.SyncRoot)
{
    // Some operation on the collection, which is now thread safe.
}
Dim myCollection As ICollection = someCollection
SyncLock myCollection.SyncRoot
    ' Some operation on the collection, which is now thread safe.
End SyncLock

枚举整个集合本质上不是一个线程安全的过程。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.

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

适用于

另请参阅