Freigeben über


Queue.Synchronized-Methode

Gibt einen synchronisierten (threadsicheren) Queue-Wrapper zurück.

Namespace: System.Collections
Assembly: mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Shared Function Synchronized ( _
    queue As Queue _
) As Queue
'Usage
Dim queue As Queue
Dim returnValue As Queue

returnValue = Queue.Synchronized(queue)
public static Queue Synchronized (
    Queue queue
)
public:
static Queue^ Synchronized (
    Queue^ queue
)
public static Queue Synchronized (
    Queue queue
)
public static function Synchronized (
    queue : Queue
) : Queue

Parameter

  • queue
    Die Queue, die synchronisiert werden soll.

Rückgabewert

Ein Queue-Wrapper, der synchronisiert (threadsicher) ist.

Ausnahmen

Ausnahmetyp Bedingung

ArgumentNullException

queue ist NULL (Nothing in Visual Basic).

Hinweise

Hinweis

Das auf diese Methode angewendete HostProtectionAttribute-Attribut besitzt den Resources-Eigenschaftenwert Synchronization. Das HostProtectionAttribute hat keine Auswirkungen auf Desktopanwendungen, die normalerweise durch Doppelklicken auf ein Symbol, Eingeben eines Befehls oder eines URL in einem Browser gestartet werden. Weitere Informationen finden Sie unter der HostProtectionAttribute-Klasse oder unter SQL Server-Programmierung und Hostschutzattribute.

Zur Gewährleistung der Threadsicherheit der Queue müssen alle Operationen unter Verwendung dieses Wrappers ausgeführt werden.

Die Enumeration einer Auflistung ist systemintern keine threadsichere Prozedur. Selbst wenn eine Auflistung synchronisiert ist, besteht die Möglichkeit, dass andere Threads sie ändern. Dies führt dazu, dass der Enumerator eine Ausnahme auslöst. Sie können während der Enumeration Threadsicherheit gewährleisten, indem Sie entweder die Auflistung während der gesamten Enumeration sperren oder die Ausnahmen abfangen, die durch Änderungen ausgelöst werden, die von anderen Threads vorgenommen werden.

Beispiel

Das folgende Codebeispiel veranschaulicht, wie die Auflistung mithilfe von SyncRoot während der gesamten Enumeration gesperrt wird: Diese Methode ist eine O(1)-Operation.

Queue myCollection = new Queue();
  lock(myCollection.SyncRoot) {
  foreach (Object item in myCollection) {
  // Insert your code here.
  }
 }
Dim myCollection As New Queue()
 Dim item As Object
 SyncLock myCollection.SyncRoot
  For Each item In myCollection
  ' Insert your code here.
  Next item
 End SyncLock

Im folgenden Beispiel wird gezeigt, wie Sie eine Queue synchronisieren und eine synchronisierte Queue verwenden und wie Sie ermitteln, ob eine Queue synchronisiert ist.

Imports System
Imports System.Collections

Public Class SamplesQueue    
    
    Public Shared Sub Main()
        
        ' Creates and initializes a new Queue.
        Dim myQ As New Queue()
        myQ.Enqueue("The")
        myQ.Enqueue("quick")
        myQ.Enqueue("brown")
        myQ.Enqueue("fox")
        
        ' Creates a synchronized wrapper around the Queue.
        Dim mySyncdQ As Queue = Queue.Synchronized(myQ)
        
        ' Displays the sychronization status of both Queues.
        Dim msg As String
        If myQ.IsSynchronized Then
            msg = "synchronized"
        Else
            msg = "not synchronized"
        End If
        Console.WriteLine("myQ is {0}.", msg)
        If mySyncdQ.IsSynchronized Then
            msg = "synchronized"
        Else
            msg = "not synchronized"
        End If
        Console.WriteLine("mySyncdQ is {0}.", msg)
    End Sub
End Class

' This code produces the following output.
' 
' myQ is not synchronized.
' mySyncdQ is synchronized. 
using System;
using System.Collections;
public class SamplesQueue  {

   public static void Main()  {

      // Creates and initializes a new Queue.
      Queue myQ = new Queue();
      myQ.Enqueue( "The" );
      myQ.Enqueue( "quick" );
      myQ.Enqueue( "brown" );
      myQ.Enqueue( "fox" );

      // Creates a synchronized wrapper around the Queue.
      Queue mySyncdQ = Queue.Synchronized( myQ );

      // Displays the sychronization status of both Queues.
      Console.WriteLine( "myQ is {0}.", myQ.IsSynchronized ? "synchronized" : "not synchronized" );
      Console.WriteLine( "mySyncdQ is {0}.", mySyncdQ.IsSynchronized ? "synchronized" : "not synchronized" );
   }
}
/* 
This code produces the following output.

myQ is not synchronized.
mySyncdQ is synchronized.
*/ 
#using <system.dll>

using namespace System;
using namespace System::Collections;
int main()
{
   
   // Creates and initializes a new Queue.
   Queue^ myQ = gcnew Queue;
   myQ->Enqueue( "The" );
   myQ->Enqueue( "quick" );
   myQ->Enqueue( "brown" );
   myQ->Enqueue( "fox" );
   
   // Creates a synchronized wrapper around the Queue.
   Queue^ mySyncdQ = Queue::Synchronized( myQ );
   
   // Displays the sychronization status of both Queues.
   Console::WriteLine( "myQ is {0}.", myQ->IsSynchronized ? (String^)"synchronized" : "not synchronized" );
   Console::WriteLine( "mySyncdQ is {0}.", mySyncdQ->IsSynchronized ? (String^)"synchronized" : "not synchronized" );
}

/*
This code produces the following output.

myQ is not synchronized.
mySyncdQ is synchronized.
*/
import System.*;
import System.Collections.*;

public class SamplesQueue
{
    public static void main(String[] args)
    {
        // Creates and initializes a new Queue.
        Queue myQ =  new Queue();
        myQ.Enqueue("The");
        myQ.Enqueue("quick");
        myQ.Enqueue("brown");
        myQ.Enqueue("fox");

        // Creates a synchronized wrapper around the Queue.
        Queue mySyncdQ = Queue.Synchronized(myQ);

        // Displays the sychronization status of both Queues.
        Console.WriteLine("myQ is {0}.",
            (myQ.get_IsSynchronized()) ? "synchronized" : "not synchronized");
        
        Console.WriteLine("mySyncdQ is {0}.",(mySyncdQ.get_IsSynchronized()) ?
            "synchronized" : "not synchronized");
    } //main
} //SamplesQueue

/* 
 This code produces the following output.
 
 myQ is not synchronized.
 mySyncdQ is synchronized.
 */

Plattformen

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

Siehe auch

Referenz

Queue-Klasse
Queue-Member
System.Collections-Namespace
IsSynchronized
SyncRoot