Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Retrieving Messages

You can use either static or dynamic retrieval mechanisms to retrieve a list of messages. When you retrieve a static list of messages, the system returns an array of Message objects representing all messages in the queue.

When you retrieve a dynamic list of messages, the system returns a MessageEnumerator object representing all messages in the queue. A MessageEnumerator object is a cursor, initialized to the head of a dynamic list. The list order is the same as the order of the messages in the queue, according to message priority. You can move the cursor to the first message in the queue by calling the MoveNext method. After the enumerator has been initialized, you can use MoveNext to step forward through the remaining messages. You can specify whether to wait for a message to become available by passing a time-out into the MoveNext method.

Because the enumerator is dynamic, it can access a message that is appended beyond the cursor's current position (for example, due to low priority). The enumerator cannot access a message that is inserted before the cursor's current position. It is not possible to step backward with a MessageEnumerator. A cursor allows forward-only movement. The Reset method enables you to place the cursor back at the beginning of the queue.

Note   If you instantiate a MessageQueue with DenySharedReceive set to true, no other application can modify the messages in your enumerator while you have the connection to the queue.

To retrieve a static list of messages

  1. Create an instance of the MessageQueue component and set its Path property to the queue to which you want to refer. For more information, see Creating MessageQueue Component Instances.
    Note   If you created your component from Server Explorer, the Path property is set automatically to the queue path for that queue.
  2. Create an array that references the Message class to hold the results of your query.
  3. Call the GetAllMessages method.
  4. Assign the results to the array you created.

    The following code shows how you might use the GetAllMessages function to retrieve the messages from a queue and display the message labels in a list box. This example assumes that a MessageQueue component instance has been added to the application.

    ' Visual Basic
    Private Sub button1_Click(ByVal sender As System.Object, _
       ByVal e As System.EventArgs) Handles button1.Click
       Me.MessageQueue1.Path = ".\MyQueue"
       Dim msg() As System.Messaging.Message
       Dim i As Integer
       ' Retrieve the messages.
       msg = MessageQueue1.GetAllMessages()
       ' Clear the current contents of the list.
       Me.ListBox1.Items.Clear()
       ' Display the results.
       For i = 0 To msg.Length - 1
          Me.ListBox1.Items.Add(msg(i).Label)
       Next
    End Sub
    
    // C#
    private void button1_Click(System.Object sender, System.EventArgs e)
    {
       messageQueue1.Path = ".\\MyQueue";
       System.Messaging.Message[] msg;
       // Retrieve the messages.
       msg = messageQueue1.GetAllMessages();
       // Clear the current contents of the list.
       this.listBox1.Items.Clear();
       // Display the results.
       for (int i = 0; i < msg.Length; i++)
       { 
          this.listBox1.Items.Add(msg[i].Label);
       }
    }
    

To retrieve a dynamic list of messages

  1. Create an instance of the MessageQueue component and set its Path property to the queue to which you want to refer. For more information, see Creating MessageQueue Component Instances.
    Note   If you created your component from Server Explorer, the Path property is set automatically to the queue path for that queue.
  2. Create a MessageEnumerator object to hold the results of your query.
  3. Call the GetEnumerator method on the MessageQueue class.
  4. Assign the results to the MessageEnumerator object. Your code might look like this:
    ' Visual Basic
    Dim mq As New System.Messaging.MessageQueue(".\MyQueue") 
    Dim msgEnum As System.Messaging.MessageEnumerator
    msgEnum = CType(mq.GetEnumerator, System.Messaging.MessageEnumerator)
    
    // C#
    System.Messaging.MessageQueue mq = new System.Messaging.MessageQueue();
    System.Messaging.MessageEnumerator msgEnum;
    mq.Path = @".\MyQueue";
    msgEnum = (System.Messaging.MessageEnumerator)(mq.GetEnumerator());
    

See Also

Queue and Message Collections | Retrieving Queues

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft