ConcurrentQueue<(Of <(T>)>) Class
This page is specific to:.NET Framework Version:
4
.NET Framework Class Library
ConcurrentQueue<(Of <(T>)>) Class

Represents a thread-safe first in-first out (FIFO) collection.

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

'Usage

Dim instance As ConcurrentQueue(Of T)

'Declaration

<SerializableAttribute> _
<ComVisibleAttribute(False)> _
<HostProtectionAttribute(SecurityAction.LinkDemand, Synchronization := True,  _
    ExternalThreading := True)> _
Public Class ConcurrentQueue(Of T) _
    Implements IProducerConsumerCollection(Of T), IEnumerable(Of T),  _
    ICollection, IEnumerable

Type Parameters

T

The type of the elements contained in the queue.

Remarks

NoteNote

The HostProtectionAttribute attribute applied to this type or member has the following Resources property value: Synchronization | ExternalThreading. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes.

Examples

The following example shows how to use a ConcurrentQueue<(Of <(T>)>) to enqueue and dequeue items:

Imports System.Collections.Concurrent
Imports System.Threading
Imports System.Threading.Tasks

Class TestQueue


    ' Demonstrates:
    ' ConcurrentQueue<T>.Enqueue()
    ' ConcurrentQueue<T>.TryPeek()
    ' ConcurrentQueue<T>.TryDequeue()
    Shared Sub Main()
        ' Construct a ConcurrentQueue
        Dim cq As New ConcurrentQueue(Of Integer)()

        ' Populate the queue
        For i As Integer = 0 To 9999
            cq.Enqueue(i)
        Next

        ' Peek at the first element
        Dim result As Integer
        If Not cq.TryPeek(result) Then
            Console.WriteLine("CQ: TryPeek failed when it should have succeeded")
        ElseIf result <> 0 Then
            Console.WriteLine("CQ: Expected TryPeek result of 0, got {0}", result)
        End If

        Dim outerSum As Integer = 0

        ' An action to consume the ConcurrentQueue
        Dim action As Action =
            Sub()
                Dim localValue As Integer
                Dim localSum As Integer = 0
                While cq.TryDequeue(localValue)
                    localSum += 1
                End While
                Interlocked.Add(outerSum, localSum)
            End Sub

        ' Start 4 concurrent consuming actions
        Parallel.Invoke(action, action, action, action)

        Console.WriteLine("outerSum = {0}, should be 10000", outerSum)
    End Sub

End Class


Inheritance Hierarchy

System..::.Object
  System.Collections.Concurrent..::.ConcurrentQueue<(Of <(T>)>)
Thread Safety

All public and protected members of ConcurrentQueue<(Of <(T>)>) are thread-safe and may be used concurrently from multiple threads.

Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 4

.NET Framework Client Profile

Supported in: 4
See Also

Reference

© 2010 Microsoft Corporation. All rights reserved.   Terms of Use | Trademarks | Privacy Statement
Page view tracker
Rate the Lightweight library
x
Lightweight builds on ScriptFree (loband) by adding features you've requested: a SearchBox and default code language selection.
Do you like the SearchBox?
Do you like the tabbed code blocks?
How useful is this topic?
Tell us more.
Thanks
x
You're helping to improve MSDN Online.
Feedback
Switch View
Classic
Lightweight Beta
ScriptFree
Switch View