SearchResultCollection.ICollection.SyncRoot 屬性

定義

取得可用以同步存取 ICollection 的物件。Gets an object that can be used to synchronize access to the ICollection.

property System::Object ^ System::Collections::ICollection::SyncRoot { System::Object ^ get(); };
object System.Collections.ICollection.SyncRoot { get; }
 ReadOnly Property SyncRoot As Object Implements ICollection.SyncRoot

屬性值

可用以同步存取 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 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 like this:

ICollection MyCollection =...  
 lock(MyCollection.SyncRoot) {  
  // Some operation on the collection, which is now thread safe.  
 }  
Dim myCollection as New ICollection()  
 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.

[Visual Basic, C#][Visual Basic, C#]

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

ICollection myCollection = new ICollection();  
 lock(myCollection.SyncRoot) {  
  foreach (Object item in myCollection) {  
  // Insert your code here.  
  }  
 }  
Dim myCollection As New ICollection()  
 Dim item As Object  
 SyncLock myCollection.SyncRoot  
  For Each item In myCollection  
  ' Insert your code here.  
  Next item  
 End SyncLock  

適用於

另請參閱