Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

MessageQueue.GetMessageEnumerator Method

Creates an enumerator object for all the messages in the queue.

[Visual Basic]
Public Function GetMessageEnumerator() As MessageEnumerator
[C#]
public MessageEnumerator GetMessageEnumerator();
[C++]
public: MessageEnumerator* GetMessageEnumerator();
[JScript]
public function GetMessageEnumerator() : MessageEnumerator;

Return Value

The MessageEnumerator holding the messages that are contained in the queue.

Remarks

GetMessageEnumerator creates a dynamic list of all the messages in a queue. You can remove from the queue the message at the enumerator's current position by calling RemoveCurrent for the MessageEnumerator that GetMessageEnumerator returns.

Because the cursor is associated with the dynamic list of messages in the queue, the enumeration reflects any modification you make to the messages in the queue, if the message is beyond the current cursor position. For example, the enumerator can automatically access a lower-priority message placed beyond the cursor's current position, but not a higher-priority message inserted before that position. However, you can reset the enumeration, thereby moving the cursor back to the beginning of the list, by calling Reset for the MessageEnumerator.

Note   The order of the messages in the enumeration reflects their order in the queue, so higher-priority messages will appear before lower-priority ones.

If you want a static snapshot of the messages in the queue rather than a dynamic connection to them, call GetAllMessages. This method returns an array of Message objects, which represent the messages at the time the method was called.

The following table shows whether this method is available in various Workgroup modes.

Workgroup Mode Available
Local computer Yes
Local computer + direct format name Yes
Remote computer Yes
Remote computer + direct format name Yes

Example

[Visual Basic, C#, C++] The following example gets a dynamic list of messages in a queue and counts all messages with the Priority property set to MessagePriority.Lowest.

[Visual Basic] 
Imports System
Imports System.Messaging

Namespace MyProject

    '/ <summary>
    '/ Provides a container class for the example.
    '/ </summary>
    Public Class MyNewQueue


        '**************************************************
        ' Provides an entry point into the application.
        '         
        ' This example uses a cursor to step through the
        ' messages in a queue and counts the number of 
        ' Lowest priority messages.
        '**************************************************

        Public Shared Sub Main()

            ' Create a new instance of the class.
            Dim myNewQueue As New MyNewQueue()

            ' Output the count of Lowest priority messages.
            myNewQueue.CountLowestPriority()

            Return

        End Sub 'Main


        '**************************************************
        ' Iterates through messages in a queue and examines
        ' their priority.
        '**************************************************

        Public Sub CountLowestPriority()

            ' Holds the count of Lowest priority messages.
            Dim numberItems As Int32 = 0

            ' Connect to a queue.
            Dim myQueue As New MessageQueue(".\myQueue")

            ' Get a cursor into the messages in the queue.
            Dim myEnumerator As MessageEnumerator = _
                myQueue.GetMessageEnumerator()

            ' Specify that the messages's priority should be read.
            myQueue.MessageReadPropertyFilter.Priority = True

            ' Move to the next message and examine its priority.
            While myEnumerator.MoveNext()

                ' Increase the count if the priority is Lowest.
                If myEnumerator.Current.Priority = _
                    MessagePriority.Lowest Then
                    numberItems += 1
                End If

            End While

            ' Display final count.
            Console.WriteLine(("Lowest priority messages: " + _
                numberItems.ToString()))

            Return

        End Sub 'CountLowestPriority

    End Class 'MyNewQueue
End Namespace 'MyProject

[C#] 
using System;
using System.Messaging;

namespace MyProject
{
    /// <summary>
    /// Provides a container class for the example.
    /// </summary>
    public class MyNewQueue
    {

        //**************************************************
        // Provides an entry point into the application.
        //         
        // This example uses a cursor to step through the
        // messages in a queue and counts the number of 
        // Lowest priority messages.
        //**************************************************

        public static void Main()
        {
            // Create a new instance of the class.
            MyNewQueue myNewQueue = new MyNewQueue();

            // Output the count of Lowest priority messages.
            myNewQueue.CountLowestPriority();
                        
            return;
        }


        //**************************************************
        // Iterates through messages in a queue and examines
        // their priority.
        //**************************************************
        
        public void CountLowestPriority()
        {
            // Holds the count of Lowest priority messages.
            uint numberItems = 0;

            // Connect to a queue.
            MessageQueue myQueue = new MessageQueue(".\\myQueue");
    
            // Get a cursor into the messages in the queue.
            MessageEnumerator myEnumerator = 
                myQueue.GetMessageEnumerator();

            // Specify that the messages's priority should be read.
            myQueue.MessageReadPropertyFilter.Priority = true;

            // Move to the next message and examine its priority.
            while(myEnumerator.MoveNext())
            {
                // Increase the count if priority is Lowest.
                if(myEnumerator.Current.Priority == 
                    MessagePriority.Lowest)
                    
                    numberItems++;
            }

            // Display final count.
            Console.WriteLine("Lowest priority messages: " + 
                numberItems.ToString());
            
            return;
        }
    }
}

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

using namespace System;
using namespace System::Messaging;

__gc class MyNewQueue 
{
public:
    void CountLowestPriority() 
    {
        // Holds the count of Lowest priority messages.
        UInt32 numberItems = 0;

        // Connect to a queue.
        MessageQueue* myQueue = new MessageQueue(S".\\myQueue");

        // Get a cursor into the messages in the queue.
        MessageEnumerator* myEnumerator = 
            myQueue->GetMessageEnumerator();

        // Specify that the messages's priority should be read.
        myQueue->MessageReadPropertyFilter->Priority = true;

        // Move to the next message and examine its priority.
        while(myEnumerator->MoveNext())
        {
            // Increase the count if priority is Lowest.
            if (myEnumerator->Current->Priority == 
                MessagePriority::Lowest)

                numberItems++;
        }

        // Display final count.
        Console::WriteLine(S"Lowest priority messages: {0}", __box(numberItems));

        return;
    }
};

int main() 
{
    // Create a new instance of the class.
    MyNewQueue* myNewQueue = new MyNewQueue();

    // Output the count of Lowest priority messages.
    myNewQueue->CountLowestPriority();

    return 0;
}

[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

.NET Framework Security: 

See Also

MessageQueue Class | MessageQueue Members | System.Messaging Namespace | GetAllMessages | GetEnumerator | GetMessageQueueEnumerator

Show:
© 2014 Microsoft. All rights reserved.