Export (0) Print
Expand All

Queue.Synchronized Method

Returns a Queue wrapper that is synchronized (thread-safe).

[Visual Basic]
Public Shared Function Synchronized( _
   ByVal queue As Queue _
) As Queue
[C#]
public static Queue Synchronized(
 Queue queue
);
[C++]
public: static Queue* Synchronized(
 Queue* queue
);
[JScript]
public static function Synchronized(
   queue : Queue
) : Queue;

Parameters

queue
The Queue to synchronize.

Return Value

A Queue wrapper that is synchronized (thread-safe).

Exceptions

Exception Type Condition
ArgumentNullException queue is a null reference (Nothing in Visual Basic).

Remarks

To guarantee the thread safety of the Queue, all operations must be done through this wrapper only.

Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads could still modify the collection, which causes the enumerator to throw an exception. To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.

[Visual Basic, C#] The following code example shows how to lock the collection using the SyncRoot during the entire enumeration:

[C#] 
Queue myCollection = new Queue();
 lock( myCollection.SyncRoot ) {
 foreach ( Object item in myCollection ) {
 // Insert your code here.
 }
}
[Visual Basic] 
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

Example

[Visual Basic, C#, C++] The following example shows how to synchronize a Queue, determine if a Queue is synchronized and use a synchronized Queue.

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

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

[C++] 
#using <mscorlib.dll>
#using <system.dll>

using namespace System;
using namespace System::Collections;

int main()  {

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

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

   // Displays the sychronization status of both Queues.
   Console::WriteLine( S"myQ is {0}.", myQ->IsSynchronized ? S"synchronized" : S"not synchronized" );
   Console::WriteLine( S"mySyncdQ is {0}.", mySyncdQ->IsSynchronized ? S"synchronized" : S"not synchronized" );
}

/*
This code produces the following output.

myQ is not synchronized.
mySyncdQ is synchronized.
*/

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

Queue Class | Queue Members | System.Collections Namespace | IsSynchronized | SyncRoot

Show:
© 2014 Microsoft