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.
To create a message queue on your computer
- On the File menu, point to New, and then click Project.
- 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.
- Open Server Explorer. For more information, see Accessing and Initializing Server Explorer .
- Expand the Servers node.
- Expand the node for your local server. The node for your local server is identified by the computer name.
- Expand the Message Queues node.
- Right-click Private Queues and select Create Queue from the shortcut menu.
- Enter HelpRequest for the queue name. Do not check Make Transactional.
- 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
- 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.
- Set the Name property of the MessageQueue component to helpRequestQueue.
- 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
- 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
- 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 |
| DataGrid1 | Name | messageGrid |
- Arrange the controls in an orderly way.
To send a message to the queue
- In the designer, double-click the sendMessage button to create the Click event handler in the Code Editor.
- 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();
}
- 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
- In the designer, double-click the refreshMessage button to create the Click event handler in the Code Editor.
- 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
- In the designer, double-click the purgeMessage button to create the Click event handler in the Code Editor.
- 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
- Press F5 to run the application.
- Enter your name and a short message.
- Click Send message to send the message to the queue and update the display.
- 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