Linguaggio

ForEachItems.IsSynchronized Proprietà

Definizione

Restituisce un booleano che indica se l'accesso alla ForEachItems collezione è sincronizzato (thread-safe).

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

Valore della proprietà

Vero se l'accesso alla collezione è sincronizzato (thread-safe); altrimenti, falso. Il valore predefinito è false.

Implementazioni

Esempio

La ArrayList è una classe framework .NET che eredita e implementa la IsSynchronized proprietà. Nell'esempio di codice seguente viene illustrato come sincronizzare un oggetto ArrayList, determinare se un oggetto ArrayList è sincronizzato e usare un oggetto sincronizzato ArrayList.

using System;  
using System.Collections;  
public class SamplesArrayList    
{  
   public static void Main()    
   {  
      // Creates and initializes a new ArrayList.  
      ArrayList myAL = new ArrayList();  
      myAL.Add( "The" );  
      myAL.Add( "quick" );  
      myAL.Add( "brown" );  
      myAL.Add( "fox" );  

      // Creates a synchronized wrapper around the ArrayList.  
      ArrayList mySyncdAL = ArrayList.Synchronized( myAL );  

      // Displays the sychronization status of both ArrayLists.  
      Console.WriteLine( "myAL is {0}.", myAL.IsSynchronized ? "synchronized" : "not synchronized" );  
      Console.WriteLine( "mySyncdAL is {0}.", mySyncdAL.IsSynchronized ? "synchronized" : "not synchronized" );  
   }  
}  
Imports System  
Imports System.Collections  
Public Class SamplesArrayList  
   Public Shared  Sub Main()  
      ' Creates and initializes a new ArrayList.  
      Dim myAL As ArrayList =  New ArrayList()   
      myAL.Add("The")  
      myAL.Add("quick")  
      myAL.Add("brown")  
      myAL.Add("fox")  

      ' Creates a synchronized wrapper around the ArrayList.  
      Dim mySyncdAL As ArrayList =  ArrayList.Synchronized(myAL)   

      ' Displays the sychronization status of both ArrayLists.  
      Console.WriteLine("myAL is {0}.", myAL.IsSynchronized ? "synchronized" : "not synchronized")  
      Console.WriteLine("mySyncdAL is {0}.", mySyncdAL.IsSynchronized ? "synchronized" : "not synchronized")  
   End Sub  
End Class  

Output di esempio:

myAL non è sincronizzato.

mySyncdAL è sincronizzato.

Commenti

Implementa ICollection.IsSynchronized. Se una raccolta è thread-safe, la IsSynchronized proprietà restituisce truee il programmatore non deve eseguire alcuna operazione per mantenere il thread-safe della raccolta.

Se la proprietà restituisce false, allora la proprietà SyncRoot restituisce un oggetto che può essere usato con la parola chiave C# lock. Per ulteriori informazioni, vedi ICollection.IsSynchronized.

Si applica a