Assembly : mscorlib (dans mscorlib.dll)
SyntaxePublic Shared Function Synchronized ( _ stack As Stack _ ) As Stack
Dim stack As Stack Dim returnValue As Stack returnValue = Stack.Synchronized(stack)
public static Stack Synchronized ( Stack stack )
public: static Stack^ Synchronized ( Stack^ stack )
public static Stack Synchronized ( Stack stack )
public static function Synchronized ( stack : Stack ) : Stack
Paramètres
- stack
Stack à synchroniser.
Valeur de retour
Wrapper synchronisé (thread-safe) autour de Stack.
Notes Remarque |
|---|
| L'attribut HostProtectionAttribute appliqué à cette méthode a la valeur de propriété Resources suivante : Synchronization. HostProtectionAttribute n'affecte pas les applications bureautiques, qui sont généralement démarrées en double-cliquant sur une icône, en tapant une commande ou en entrant une URL dans un navigateur. Pour plus d'informations, consultez la classe HostProtectionAttribute ou Attributs de programmation et de protection des hôtes SQL Server. |
Afin de garantir la sécurité des threads de Stack, toutes les opérations doivent être effectuées par l'intermédiaire de ce wrapper.
L'énumération d'une collection n'est intrinsèquement pas une procédure thread-safe. Même lorsqu'une collection est synchronisée, les autres threads peuvent toujours modifier la collection, ce qui entraîne la levée d'une exception par l'énumérateur. Pour garantir la sécurité des threads au cours de l'énumération, vous pouvez soit verrouiller la collection pendant toute l'énumération, soit intercepter les exceptions résultant des modifications apportées par les autres threads.
L'exemple de code suivant montre comment verrouiller la collection à l'aide de SyncRoot au cours de l'énumération entière.
Stack myCollection = new Stack(); lock(myCollection.SyncRoot) { foreach (Object item in myCollection) { // Insert your code here. } }
Dim myCollection As New Stack() Dim item As Object SyncLock myCollection.SyncRoot For Each item In myCollection ' Insert your code here. Next item End SyncLock
Cette méthode est une opération O(1).
ExempleL'exemple suivant montre comment synchroniser un Stack, déterminer si un Stack est synchronisé et utiliser un Stack synchronisé.
Imports System Imports System.Collections Public Class SamplesStack Public Shared Sub Main() ' Creates and initializes a new Stack. Dim myStack As New Stack() myStack.Push("The") myStack.Push("quick") myStack.Push("brown") myStack.Push("fox") ' Creates a synchronized wrapper around the Stack. Dim mySyncdStack As Stack = Stack.Synchronized(myStack) ' Displays the sychronization status of both Stacks. Dim msg As String If myStack.IsSynchronized Then msg = "synchronized" Else msg = "not synchronized" End If Console.WriteLine("myStack is {0}.", msg) If mySyncdStack.IsSynchronized Then msg = "synchronized" Else msg = "not synchronized" End If Console.WriteLine("mySyncdStack is {0}.", msg) End Sub End Class ' This code produces the following output. ' ' myStack is not synchronized. ' mySyncdStack is synchronized.
using System; using System.Collections; public class SamplesStack { public static void Main() { // Creates and initializes a new Stack. Stack myStack = new Stack(); myStack.Push( "The" ); myStack.Push( "quick" ); myStack.Push( "brown" ); myStack.Push( "fox" ); // Creates a synchronized wrapper around the Stack. Stack mySyncdStack = Stack.Synchronized( myStack ); // Displays the sychronization status of both Stacks. Console.WriteLine( "myStack is {0}.", myStack.IsSynchronized ? "synchronized" : "not synchronized" ); Console.WriteLine( "mySyncdStack is {0}.", mySyncdStack.IsSynchronized ? "synchronized" : "not synchronized" ); } } /* This code produces the following output. myStack is not synchronized. mySyncdStack is synchronized. */
#using <system.dll> using namespace System; using namespace System::Collections; int main() { // Creates and initializes a new Stack. Stack^ myStack = gcnew Stack; myStack->Push( "The" ); myStack->Push( "quick" ); myStack->Push( "brown" ); myStack->Push( "fox" ); // Creates a synchronized wrapper around the Stack. Stack^ mySyncdStack = Stack::Synchronized( myStack ); // Displays the sychronization status of both Stacks. Console::WriteLine( "myStack is {0}.", myStack->IsSynchronized ? (String^)"synchronized" : "not synchronized" ); Console::WriteLine( "mySyncdStack is {0}.", mySyncdStack->IsSynchronized ? (String^)"synchronized" : "not synchronized" ); } /* This code produces the following output. myStack is not synchronized. mySyncdStack is synchronized. */
import System.*; import System.Collections.*; public class SamplesStack { public static void main(String[] args) { // Creates and initializes a new Stack. Stack myStack = new Stack(); myStack.Push("The"); myStack.Push("quick"); myStack.Push("brown"); myStack.Push("fox"); // Creates a synchronized wrapper around the Stack. Stack mySyncdStack = Stack.Synchronized(myStack); // Displays the sychronization status of both Stacks. Console.WriteLine("myStack is {0}.", (myStack.get_IsSynchronized()) ? "synchronized" : "not synchronized"); Console.WriteLine("mySyncdStack is {0}.", (mySyncdStack.get_IsSynchronized()) ? "synchronized" : "not synchronized"); } //main } //SamplesStack /* This code produces the following output. myStack is not synchronized. mySyncdStack is synchronized. */
Plates-formesWindows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile pour Pocket PC, Windows Mobile pour Smartphone, Windows Server 2003, Windows XP Édition Media Center, Windows XP Professionnel Édition x64, Windows XP SP2, Windows XP Starter Edition
Le .NET Framework ne prend pas en charge toutes les versions de chaque plate-forme. Pour obtenir la liste des versions prises en charge, consultez Configuration requise.
Informations de version
Remarque
Outils (masquer)
S'enregistrer
Liste des Membres
Qui est en ligne?
FAQ