Megosztás a következőn keresztül:


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. By 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 Windows component on the client computer. To add this service, use Add or Remove Programs in Control Panel.

Note

Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. 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 or Visual C# 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 the local server. The node for the local server is identified by the computer name.

  6. Expand the Message Queues node.

  7. Right-click Private Queues and then click Create Queue.

  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.

    Note

    You can also view the newly created queue through the My Computer icon. Right-click the My Computer icon on your desktop and then click Manage. 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();
    }
    
  3. Add a reference to System.Data to your project. Add an Imports Statement (.NET Namespace and Type) (Visual Basic) or using (C# Reference) statement for System.Data. For more information, see Managing References, and How to: Add or Remove References in Visual Studio (Visual Basic).

  4. Add a method to display the contents of the queue in the DataGridView 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 DataGridView control. To retrieve the text of the message, you have 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;
    

    }

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

    }

To clear 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();
    

    }

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.

See Also

Tasks

How to: Create Queues

How to: Create MessageQueue Component Instances

How to: Send Simple Messages

How to: Retrieve Messages

Concepts

Introduction to Messaging

Other Resources

Using Messaging Components