How to: Retrieve 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
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 How to: Create 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.
Create an array that references the Message class to hold the results of your query.
Call the GetAllMessages method.
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.
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
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); } }
This code example is also available as an IntelliSense code snippet. In the code snippet picker, it is located in Windows Operating System > Message Queues. For more information, see How to: Insert Snippets Into Your Code (Visual Basic).
To retrieve a dynamic list of messages
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 How to: Create 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.
Create a MessageEnumerator object to hold the results of your query.
Call the GetEnumerator method on the MessageQueue class.
Assign the results to the MessageEnumerator object. Your code might look like this:
Dim mq As New System.Messaging.MessageQueue(".\MyQueue") Dim msgEnum As System.Messaging.MessageEnumerator msgEnum = CType(mq.GetMessageEnumerator2, System.Messaging.MessageEnumerator)
System.Messaging.MessageQueue mq = new System.Messaging.MessageQueue(); System.Messaging.MessageEnumerator msgEnum; mq.Path = @".\MyQueue"; msgEnum = (System.Messaging.MessageEnumerator)(mq.GetMessageEnumerator2());