Array.IsSynchronized Array.IsSynchronized Array.IsSynchronized Array.IsSynchronized Property

定義

取得值,這個值表示對 Array 的存取是否同步 (安全執行緒)。Gets a value indicating whether access to the Array is synchronized (thread safe).

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

屬性值

所有陣列的這個屬性一律會是 falseThis property is always false for all arrays.

實作

範例

下列程式碼範例示範如何使用在整個列舉期間鎖定陣列SyncRoot屬性。The following code example shows how to lock an array during the entire enumeration by using the SyncRoot property.

Array^ myArray = gcnew array<Int32> { 1, 2, 4 };
try
{
    Monitor::Enter(myArray->SyncRoot); 
        
    for each (Int32 item in myArray)
        Console::WriteLine(item);
}
finally
{
    Monitor::Exit(myArray->SyncRoot);
}
Array myArray = new int[] { 1, 2, 4 };
lock(myArray.SyncRoot) 
{
    foreach (Object item in myArray)
        Console.WriteLine(item);
}
Dim myArray As Array = New Integer() { 1, 2, 4 }
SyncLock(myArray.SyncRoot) 
    For Each item As Object In myArray
        Console.WriteLine(item)
    Next
End SyncLock

備註

Array 會實作IsSynchronized屬性因為它需要System.Collections.ICollection介面。Array implements the IsSynchronized property because it is required by the System.Collections.ICollection interface.

.NET framework 類別根據Array提供的集合使用自己已同步處理的版本SyncRoot屬性。.NET Framework classes based on Array provide their own synchronized version of the collection using the SyncRoot property.

使用陣列的類別也可以實作自己的同步處理使用SyncRoot屬性。Classes that use arrays can also implement their own synchronization 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. 請注意,某些實作SyncRoot可能會傳回Array本身。Note that some implementations of SyncRoot might return the Array itself.

透過集合列舉是本質上並非安全執行緒的程序。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.

擷取這個屬性的值是一種 O(1) 運算。Retrieving the value of this property is an O(1) operation.

適用於

另請參閱