Stack.Synchronized, méthode (System.Collections)

Bibliothèque de classes .NET Framework 
Stack.Synchronized, méthode 

Retourne un wrapper synchronisé (thread-safe) pour Stack.

Espace de noms : System.Collections
Assembly : mscorlib (dans mscorlib.dll)

SyntaxeSyntaxe


Visual Basic (Déclaration)
Public Shared Function Synchronized ( _
    stack As Stack _
) As Stack


Visual Basic (Utilisation)
Dim stack As Stack
Dim returnValue As Stack

returnValue = Stack.Synchronized(stack)


C#
public static Stack Synchronized (
    Stack stack
)


C++
public:
static Stack^ Synchronized (
    Stack^ stack
)


J#
public static Stack Synchronized (
    Stack stack
)


JScript
public static function Synchronized (
    stack : Stack
) : Stack

Paramètres

stack

Stack à synchroniser.

Valeur de retour

Wrapper synchronisé (thread-safe) autour de Stack.
ExceptionsExceptions
Type d'exceptionCondition

ArgumentNullException

stack est référence Null (Nothing en Visual Basic).

NotesNotes
RemarqueRemarque

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.



C#
Stack myCollection = new Stack();
  lock(myCollection.SyncRoot) {
  foreach (Object item in myCollection) {
  // Insert your code here.
  }
 }


Visual Basic
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).

ExempleExemple

L'exemple suivant montre comment synchroniser un Stack, déterminer si un Stack est synchronisé et utiliser un Stack synchronisé.



Visual Basic
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.
 


C#
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.
 */ 


C++
#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.
*/


J#
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-formesPlates-formes

Windows 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 versionInformations de version

.NET Framework

Prise en charge dans : 2.0, 1.1, 1.0

.NET Compact Framework

Prise en charge dans : 2.0, 1.0
Voir aussiVoir aussi

Ces informations proviennent du site de http://msdn2.microsoft.com
Source de cette page : http://msdn2.microsoft.com/fr-fr/library/system.collections.stack.synchronized.aspx

Réseaux sociaux

Vous pouvez modifier vos préférences dans votre profil pour ne plus afficher les interactions avec les réseaux sociaux sur ces pages.

 

Nuage de mots clés

9 mots clés dont 0 définis manuellement (plus d'information...).

Avertissement

Cette page ne possède pas encore de mots clés manuels, ceci est donc un exemple automatique (les niveaux de pertinence sont fictifs, mais les liens sont valables). Pour tester le nuage avec une page qui contient des mots définis manuellement, vous pouvez cliquer ici.

Vous pouvez modifier vos préférences dans votre profil pour ne plus afficher le nuage de mots clés.

 

Astuce pour imprimer les couleurs des cellules de tableaux : http://www.gaudry.be/ast-rf-450.html
Aucun commentaire pour cette page

© Ce document issu de l′infobrol est enregistré sous le certificat Cyber PrInterDeposit Digital Numbertection. Enregistrement IDDN n° 5329-6304
Document créé le 30/10/06 02:58, dernière modification le Vendredi 17 Juin 2011, 12:11
Source du document imprimé : http://www.gaudry.be/dotnet-rf-system.collections.stack.synchronized.html Document affiché 4 fois ce mois de Mai.
St.Gaudry©07.01.02
Outils (masquer)
||
Recherche (afficher)
Recherche :

Utilisateur (masquer)
Apparence (afficher)
Stats (afficher)
15832 documents
452 astuces.
549 niouzes.
3099 definitions.
447 membres.
8115 messages.

Document genere en :
0,41 seconde

Mises à jour :
Mises à jour du site
Citation (masquer)
Windows ne demande jamais à Chuck Norris d'envoyer le rapport d'erreur. Bill Gates vient le chercher lui même, avec toutes ses excuses.

Anonyme [Chuck Norris fact]
 
l'infobrol
Nous sommes le Jeudi 31 Mai 2012, 18:53, toutes les heures sont au format GMT+1.00 Heure, heure d'été (+1)