ControlCollection.SyncRoot Propiedad

Definición

Obtiene un objeto que puede utilizarse para sincronizar el acceso a la colección de controles.Gets an object that can be used to synchronize access to the collection of controls.

public:
 property System::Object ^ SyncRoot { System::Object ^ get(); };
public object SyncRoot { get; }
member this.SyncRoot : obj
Public ReadOnly Property SyncRoot As Object

Valor de propiedad

Object

Objeto Object que se utiliza para sincronizar la colección.The Object used to synchronize the collection.

Implementaciones

Ejemplos

En el ejemplo de código siguiente se crea un método que enumera la ControlCollection colección de un Button control, myButton .The following code example creates a method that enumerates through the ControlCollection collection of a Button control, myButton. Cuando se crea el enumerador, IsSynchronized se comprueba la propiedad para ver si la operación es segura para subprocesos y, en caso contrario, se SyncRoot utiliza la propiedad para obtener un objeto con el fin de que la operación sea segura para subprocesos.When the enumerator is created, the IsSynchronized property is checked to see if the operation is thread safe, and if it is not, the SyncRoot property is used to obtain an object to make the operation thread safe. Una vez completada la enumeración, el valor de la IsReadOnly propiedad se escribe como la Text propiedad de un Label control en la página contenedora.When the enumeration is complete, the value of the IsReadOnly property is written as the Text property of a Label control on the containing page.

// Create a method that enuberates through a 
// button//s ControlCollection in a thread-safe manner.  
public void ListControlCollection(object sender, EventArgs e)
{
   IEnumerator myEnumerator = myButton.Controls.GetEnumerator();

   // Check the IsSynchronized property. If False,
   // use the SyncRoot method to get an object that 
   // allows the enumeration of all controls to be 
   // thread safe.
   if (myButton.Controls.IsSynchronized == false)
   {
       lock (myButton.Controls.SyncRoot)
       {
           while (myEnumerator.MoveNext())
           {

               Object myObject = myEnumerator.Current;

               LiteralControl childControl = (LiteralControl)myEnumerator.Current;
               Response.Write("<b><br /> This is the  text of the child Control  </b>: " +
                              childControl.Text);
           }
           msgReadOnly.Text = myButton.Controls.IsReadOnly.ToString();
       }
   }       
}
' Create a method that enuberates through a 
' button's ControlCollection in a thread-safe manner.  
Public Sub ListControlCollection(sender As Object, e As EventArgs)
   Dim myEnumerator As IEnumerator = myButton.Controls.GetEnumerator()

   ' Check the IsSynchronized property. If False,
   ' use the SyncRoot method to get an object that 
   ' allows the enumeration of all controls to be 
   ' thread safe.
   If myButton.Controls.IsSynchronized = False Then
     SyncLock myButton.Controls.SyncRoot
       While (myEnumerator.MoveNext())

       Dim myObject As Object  = myEnumerator.Current
           
         Dim childControl As LiteralControl = CType(myEnumerator.Current, LiteralControl)
         Response.Write("<b><br /> This is the  text of the child Control  </b>: " & _
                        childControl.Text)
       End While
      msgReadOnly.Text = myButton.Controls.IsReadOnly.ToString()
      
      End SyncLock
   End If       
 End Sub

Se aplica a

Consulte también