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.

Walkthrough: Creating a Queue and Working with Messages

The procedures in this topic demonstrate the process of creating a message queue using the MessageQueue component. Using that component, you send messages to the queue and retrieve messages from it. The messages log help requests.

Note   To see queue information in Server Explorer or to access queues programmatically, you must install the Message Queuing Services Windows component on your client computer. To add this service, use the Add/Remove Programs tool in the Control Panel.
Note   The Servers node of Server Explorer is not available in the Standard Edition of Visual Basic and Visual C# .NET. For more information, see Visual Basic Standard Edition Features or Visual C# Standard Edition Features.

To create a message queue on your computer

  1. On the File menu, point to New, and then click Project.
  2. In the New Project dialog box, choose Visual Basic or Visual C# in the left pane, then choose the Windows Application template. Name it MessageQ.
  3. Open Server Explorer. For more information, see Accessing and Initializing Server Explorer .
  4. Expand the Servers node.
    Note   The Servers node of Server Explorer is not available in the Standard Edition of Visual Basic and Visual C# .NET. For more information, see Visual Basic Standard Edition Features or Visual C# Standard Edition Features.
  5. Expand the node for your local server. The node for your local server is identified by the computer name.
  6. Expand the Message Queues node.
  7. Right-click Private Queues and select Create Queue from the shortcut menu.
  8. Enter HelpRequest for the queue name. Do not check Make Transactional.
  9. A new private queue named HelpRequest is created and appears in Server Explorer.
    Note   You can also view the newly created queue through the My Computer icon. Right-click the My Computer icon on your desktop and select Manage from the shortcut menu. Expand the Services and Applications node. Expand the Message Queuing node and select the Private Queues folder. The new queue appears in the queue list.

To add a MessageQueue component for your message queue

  1. Drag the HelpRequest queue from Server Explorer onto your form. A new MessageQueue component is added to the project, configured for the HelpRequest queue.

    The MessageQueue component is used to programmatically access the messages contained in the HelpRequest queue you created in the previous section.

  2. Set the Name property of the MessageQueue component to helpRequestQueue.
  3. In the Properties window, expand the MessageReadPropertyFilter node. Set the value of Priority to true. This causes the priority of the message to be retrieved when a message is retrieved from the queue.

The user interface that you create in the next procedure allows the user to enter some text for a help request and set the priority of the message. The user clicks a Send button to send the request to the queue. A datagrid displays the contents of the queue. The user interface also contains buttons to update the grid with the current contents of the queue and purge the queue.

To create the user interface

  1. From the Windows Forms tab of the Toolbox, add the following controls to Form1:
    • Two labels
    • Two text boxes
    • Three buttons
    • One check box
    • One datagrid
  2. Set the following properties of the controls:
    ControlPropertyNew Value
    Label1TextName
    Label2TextMessage
    TextBox1NametxtName
     Text(blank)
    TextBox2NametxtMessage
     Text(blank)
     Multilinetrue
    Button1NamesendMessage
     TextSend message
    Button2NamerefreshMessages
     TextRefresh message list
    Button3NamepurgeMessages
     TextPurge message list
    CheckBox1NamehighPriority
     TextHigh priority
    DataGrid1NamemessageGrid
  3. Arrange the controls in an orderly way.

To send a message to the queue

  1. In the designer, double-click the sendMessage button to create the Click event handler in the Code Editor.
  2. Add code to the method to create a new Message instance and send it to the queue, and to update the message display. You will write the DisplayMessages method in the next step.
    ' Visual Basic
    Private Sub sendMessage_Click(ByVal sender As System.Object, _
       ByVal e As System.EventArgs) Handles sendMessage.Click
       Dim theMessage As System.Messaging.Message = _
          New System.Messaging.Message(Me.txtMessage.Text)
       theMessage.Label = Me.txtName.Text
       If highPriority.Checked Then
          theMessage.Priority = Messaging.MessagePriority.Highest
       Else
          theMessage.Priority = Messaging.MessagePriority.Normal
       End If
       helpRequestQueue.Send(theMessage)
       DisplayMessages()
    End Sub
    
    // C#
    private void sendMessage_Click(object sender, System.EventArgs e)
    {
       System.Messaging.Message theMessage = 
          new System.Messaging.Message(txtMessage.Text);
       theMessage.Label = txtName.Text; 
       if (highPriority.Checked)
          theMessage.Priority = System.Messaging.MessagePriority.Highest;
       else
          theMessage.Priority = System.Messaging.MessagePriority.Normal;
       helpRequestQueue.Send(theMessage);
       DisplayMessages();
    }
    
  3. Add a method to display the contents of the queue in the DataGrid control. This method uses the MessageQueue.GetAllMessages method to retrieve all the messages from the queue. The selected queue properties are added to a DataTable object, which is used as the data source for the DataGrid control. To retrieve the text of the message, you need to create a formatter for the message.
    ' Visual Basic
    Private Sub DisplayMessages()
       ' Create a DataTable
       Dim messageTable As New DataTable()
       messageTable.Columns.Add("Name")
       messageTable.Columns.Add("Message")
       messageTable.Columns.Add("Priority")
       Dim messages() As System.Messaging.Message
       messages = helpRequestQueue.GetAllMessages()
       ' Need a formatter to get the text of the message body.
       Dim stringFormatter As System.Messaging.XmlMessageFormatter = _
          New System.Messaging.XmlMessageFormatter(New String() _
          {"System.String"})
       Dim index As Integer
       Dim am As System.Messaging.Message
       ' Add each message to the DataTable
       For index = 0 To messages.Length - 1
          messages(index).Formatter = stringFormatter
          am = messages(index)
          messageTable.Rows.Add(New String() _
             {am.Label, am.Body.ToString(), am.Priority.ToString()})
       Next
    
       messageGrid.DataSource = messageTable
    End Sub
    
    // C#
    private void DisplayMessages()
    {
       DataTable messageTable = new DataTable();
       messageTable.Columns.Add("Name");
       messageTable.Columns.Add("Message");
       messageTable.Columns.Add("Priority");
       System.Messaging.Message[] messages;
       messages = helpRequestQueue.GetAllMessages();
       System.Messaging.XmlMessageFormatter stringFormatter;
       stringFormatter = new System.Messaging.XmlMessageFormatter(
          new string[] {"System.String"});
       for (int index = 0; index < messages.Length; index++) 
       {
          messages[index].Formatter = stringFormatter;
          messageTable.Rows.Add(new string[] {
             messages[index].Label,
             messages[index].Body.ToString(),
             messages[index].Priority.ToString() });
       }
       messageGrid.DataSource = messageTable;
    }
    

To display the contents of the queue

  1. In the designer, double-click the refreshMessage button to create the Click event handler in the Code Editor.
  2. Within the Click event handler, call the DisplayMessages method.
    ' Visual Basic
    Private Sub refreshMessages_Click(ByVal sender As System.Object, _
       ByVal e As System.EventArgs) Handles refreshMessages.Click
       DisplayMessages()
    End Sub
    
    // C#
    private void refreshMessages_Click(object sender, System.EventArgs e)
    {
       DisplayMessages();
    }
    

To purge the contents of the queue

  1. In the designer, double-click the purgeMessage button to create the Click event handler in the Code Editor.
  2. Call the Purge method of the helpRequestQueue, and then refresh the contents of the DataGrid control.
    ' Visual Basic
    Private Sub purgeMessages_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles purgeMessages.Click
       helpRequestQueue.Purge()
       DisplayMessages()
    End Sub
    
    // C#
    private void purgeMessages_Click(object sender, System.EventArgs e)
    {
       helpRequestQueue.Purge();
       DisplayMessages();
    }
    

To test the application

  1. Press F5 to run the application.
  2. Enter your name and a short message.
  3. Click Send message to send the message to the queue and update the display.
  4. Click Purge messages to delete all the messages in the queue. The list of messages will be empty.

See Also

Introduction to Messaging | Creating Queues | Creating MessageQueue Component Instances | Sending Simple Messages | Retrieving Messages | Creating Messaging Components

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