StringDictionary.SyncRoot Propriété
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Obtient un objet qui peut être utilisé pour synchroniser l’accès StringDictionaryau .
public:
virtual property System::Object ^ SyncRoot { System::Object ^ get(); };
public virtual object SyncRoot { get; }
member this.SyncRoot : obj
Public Overridable ReadOnly Property SyncRoot As Object
Valeur de propriété
Qui Object peut être utilisé pour synchroniser l’accès au StringDictionary.
Exemples
L’exemple de code suivant montre comment verrouiller la collection à l’aide de l’énumération SyncRoot entière.
StringDictionary myCollection = new StringDictionary();
lock(myCollection.SyncRoot)
{
foreach (Object item in myCollection)
{
// Insert your code here.
}
}
Dim myCollection As New StringDictionary()
SyncLock myCollection.SyncRoot
For Each item As Object In myCollection
' Insert your code here.
Next item
End SyncLock
La récupération de la valeur de cette propriété est une opération O(1).
Remarques
Les classes dérivées peuvent fournir leur propre version synchronisée de l’utilisation StringDictionary de la SyncRoot propriété. Le code synchronisant doit effectuer des opérations sur le SyncRootStringDictionary, pas directement sur le StringDictionary. Cela garantit le bon fonctionnement des collections dérivées d’autres objets. Plus précisément, il gère une synchronisation appropriée avec d’autres threads qui peuvent modifier simultanément l’objet StringDictionary .
L’énumération par le biais d’une collection n’est intrinsèquement pas une procédure thread-safe. Même lorsqu’une collection est synchronisée, d’autres threads peuvent toujours modifier la collection, ce qui provoque la levée d’une exception par l’énumérateur. Pour garantir la sécurité des threads pendant l’énumération, vous pouvez verrouiller la collection pendant toute l’énumération ou intercepter les exceptions résultant des modifications apportées par d’autres threads.