Share via


NameObjectCollectionBase.ICollection.IsSynchronized Properti

Definisi

Mendapatkan nilai yang menunjukkan apakah akses ke objek disinkronkan NameObjectCollectionBase (utas aman).

property bool System::Collections::ICollection::IsSynchronized { bool get(); };
bool System.Collections.ICollection.IsSynchronized { get; }
member this.System.Collections.ICollection.IsSynchronized : bool
 ReadOnly Property IsSynchronized As Boolean Implements ICollection.IsSynchronized

Nilai Properti

true jika akses ke objek disinkronkan NameObjectCollectionBase (utas aman); jika tidak, false. Default adalah false.

Penerapan

Keterangan

Objek NameObjectCollectionBase tidak disinkronkan. Kelas turunan dapat menyediakan versi yang disinkronkan NameObjectCollectionBase dari menggunakan SyncRoot properti .

Menghitung melalui koleksi secara intrinsik bukan prosedur aman utas. Bahkan ketika koleksi disinkronkan, utas lain masih dapat memodifikasi koleksi, yang menyebabkan enumerator melemparkan pengecualian. Untuk menjamin keamanan utas selama enumerasi, Anda dapat mengunci koleksi selama seluruh enumerasi atau menangkap pengecualian yang dihasilkan dari perubahan yang dibuat oleh utas lain.

Contoh kode berikut menunjukkan cara mengunci koleksi menggunakan SyncRoot properti selama seluruh enumerasi.

// Create a collection derived from NameObjectCollectionBase
ICollection^ myCollection = gcnew DerivedCollection();
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);
    }
}
// Create a collection derived from NameObjectCollectionBase
ICollection myCollection = new DerivedCollection();
lock(myCollection.SyncRoot)
{
    foreach (object item in myCollection)
    {
        // Insert your code here.
    }
}
' Create a collection derived from NameObjectCollectionBase
Dim myCollection As ICollection = New DerivedCollection()
SyncLock myCollection.SyncRoot
    For Each item As Object In myCollection
        ' Insert your code here.
    Next item
End SyncLock

Mengambil nilai properti ini adalah operasi O(1).

Berlaku untuk

Lihat juga