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. 然而,衍生類別 (Derived Class) 可以使用 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

適用於

另請參閱