NameObjectCollectionBase.KeysCollection.ICollection.SyncRoot 属性
定义
获取可用于同步对 NameObjectCollectionBase.KeysCollection 的访问的对象。Gets an object that can be used to synchronize access to the NameObjectCollectionBase.KeysCollection.
property System::Object ^ System::Collections::ICollection::SyncRoot { System::Object ^ get(); };
object System.Collections.ICollection.SyncRoot { get; }
member this.System.Collections.ICollection.SyncRoot : obj
ReadOnly Property SyncRoot As Object Implements ICollection.SyncRoot
属性值
用于同步对 NameObjectCollectionBase.KeysCollection 的访问的对象。An object that can be used to synchronize access to the NameObjectCollectionBase.KeysCollection.
实现
示例
下面的代码示例演示如何使用在 SyncRoot 整个枚举过程中锁定集合。The following code example shows how to lock the collection using the SyncRoot during the entire enumeration.
// Create a collection derived from NameObjectCollectionBase
NameObjectCollectionBase^ myBaseCollection = gcnew DerivedCollection();
// Get the ICollection from NameObjectCollectionBase.KeysCollection
ICollection^ myKeysCollection = myBaseCollection->Keys;
bool lockTaken = false;
try
{
Monitor::Enter(myKeysCollection->SyncRoot, lockTaken);
for each (Object^ item in myKeysCollection)
{
// Insert your code here.
}
}
finally
{
if (lockTaken)
{
Monitor::Exit(myKeysCollection->SyncRoot);
}
}
// Create a collection derived from NameObjectCollectionBase
NameObjectCollectionBase myBaseCollection = new DerivedCollection();
// Get the ICollection from NameObjectCollectionBase.KeysCollection
ICollection myKeysCollection = myBaseCollection.Keys;
lock(myKeysCollection.SyncRoot)
{
foreach (object item in myKeysCollection)
{
// Insert your code here.
}
}
' Create a collection derived from NameObjectCollectionBase
Dim myBaseCollection As NameObjectCollectionBase = New DerivedCollection()
' Get the ICollection from NameObjectCollectionBase.KeysCollection
Dim myKeysCollection As ICollection = myBaseCollection.Keys
SyncLock myKeysCollection.SyncRoot
For Each item As Object In myKeysCollection
' Insert your code here.
Next item
End SyncLock
检索此属性的值的运算复杂度为 O(1)。Retrieving the value of this property is an O(1) operation.
注解
派生类可以使用属性提供自己的同步版本 NameObjectCollectionBase.KeysCollection SyncRoot 。Derived classes can provide their own synchronized version of the NameObjectCollectionBase.KeysCollection using the SyncRoot property. 同步代码必须对的执行操作 SyncRoot NameObjectCollectionBase.KeysCollection ,而不是直接在上执行操作 NameObjectCollectionBase.KeysCollection 。The synchronizing code must perform operations on the SyncRoot of the NameObjectCollectionBase.KeysCollection, not directly on the NameObjectCollectionBase.KeysCollection. 这样可确保对从其他对象派生的集合正确地执行操作。This ensures proper operation of collections that are derived from other objects. 具体而言,它与可能同时修改对象的其他线程保持正确的同步 NameObjectCollectionBase.KeysCollection 。Specifically, it maintains proper synchronization with other threads that might be simultaneously modifying the NameObjectCollectionBase.KeysCollection object.
枚举整个集合本质上不是一个线程安全的过程。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.