Export (0) Print
Expand All

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.

NoteNote

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 Control Panel.

NoteNote

The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Visual Studio Settings.

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, create a Visual Basic, Visual C#, or Visual J# Windows Application. Name it MessageQ.

  3. Open Server Explorer. For more information, see How to: Access and Initialize Server Explorer/Database Explorer .

  4. Expand the Servers node.

  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 select Make Transactional.

  9. A new private queue named HelpRequest is created and appears in Server Explorer.

    NoteNote

    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 DataGridView control 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:

  2. Set the following properties of the controls:

    Control Property New Value

    Label1

    Text

    Name

    Label2

    Text

    Message

    TextBox1

    Name

    txtName

     

    Text

    (blank)

    TextBox2

    Name

    txtMessage

     

    Text

    (blank)

     

    Multiline

    true

    Button1

    Name

    sendMessage

     

    Text

    Send message

    Button2

    Name

    refreshMessages

     

    Text

    Refresh message list

    Button3

    Name

    purgeMessages

     

    Text

    Purge message list

    CheckBox1

    Name

    highPriority

     

    Text

    High priority

    DataGridView1

    Name

    messageGrid

  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.

    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
    
    

    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();
    }
    
    

    private void sendMessage_Click(Object sender, System.EventArgs e)
    {
    System.Messaging.Message theMessage = new System.Messaging.Message(txtMessage.get_Text());
    theMessage.set_Label(txtName.get_Text());
    if (highPriority.get_Checked())
       theMessage.set_Priority(System.Messaging.MessagePriority.Highest);
    else
       theMessage.set_Priority(System.Messaging.MessagePriority.Normal);
    
    helpRequestQueue.Send(theMessage);
    }
    
  3. Add a reference to System.Data to your project. Add an Imports Statement (Visual Basic) or using (C# Programmers Reference) statement or import (Visual J#) statement for System.Data. For more information, see Managing References, and How to: Add or Remove References in Visual Studio.

  4. Add a method to display the contents of the queue in the DataGridView control. This method uses the System.Messaging.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 DataGridView control. To retrieve the text of the message, you need to create a formatter for the message.

    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
    
    

    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;
    }
    
    

    private void DisplayMessages()
    {
       DataTable messageTable = new DataTable();
       messageTable.get_Columns().Add("Name");
       messageTable.get_Columns().Add("Message");
       messageTable.get_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].set_Formatter(stringFormatter);
          messageTable.get_Rows().Add(new System.String[] {
                               messages[index].get_Label(),
                               messages[index].get_Body().ToString(),
                               messages[index].get_Priority().ToString()});
       }
       messageGrid.set_DataSource(messageTable);
    }
    
  5. In Visual J#, add one more line of code at the end of the Click event for the sendMessage button to update the message display.

    DisplayMessages();
    

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.

    Private Sub refreshMessages_Click(ByVal sender As System.Object, _
       ByVal e As System.EventArgs) Handles refreshMessages.Click
       DisplayMessages()
    End Sub
    
    

    private void refreshMessages_Click(object sender, System.EventArgs e)
    {
       DisplayMessages();
    }
    
    

    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 DataGridView control.

    Private Sub purgeMessages_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles purgeMessages.Click
       helpRequestQueue.Purge()
       DisplayMessages()
    End Sub
    
    

    private void purgeMessages_Click(object sender, System.EventArgs e)
    {
       helpRequestQueue.Purge();
       DisplayMessages();
    }
    
    

    private void purgeMessages_Click(Object sender, System.EventArgs e)
    {
       helpRequestQueue.Purge();
       DisplayMessages();
    }
    

To test the application

  1. Press F5 to run the application.

  2. Type 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 is now empty.

Community Additions

ADD
Show:
© 2014 Microsoft