Working with Messages

The Message class represents a message in MSMQ. The Message class provides one static method called InfiniteTimeout that specifies that no timeout exists. This class provides more than 40 properties and some of them represent the body text, format, security, ID, and priority of the message. You'll see this class's properties in the sample applications throughout this chapter.

Creating and Removing Messages

You can use the Message class to create a message. Once you have created a message, you can use it as an argument of the Send and Receive methods of MessageQueue to send and receive messages. The following code creates a message and sets its priority, body, and label:

 Dim msg1 As Message = New Message() msg1.Priority = MessagePriority.High msg1.Body = "Body of first message" msg1.Label = "First Message" 

The Purge method of MessageQueue removes all the messages from a queue. The following code removes messages from MessageQueue1:


You can remove a single message from the queue by calling the Receive method of MessageQueue. You can also use the ReceiveById method to remove a message identified by the ID. You can also call the PeekById method to find a specific message and then use the RecieveById method to remove it.


You can always remove messages manually from the Server Explorer by selecting the Queue Messages Clear Messages menu option. There's no way you can remove a single message from a queue manually.

Setting Queue and Message Priorities

You can set both a queue's and a message's priority. Both MessageQueue and Message classes provide properties to set their priorities. You can use the BasePriority property of MesssageQueue to get and set a priority of a queue. It's only applied on public queues. The BasePriority of a private queue is always 0.

You can use the Priority property of the Message class to get and set the priority of a message. The MessagePriority enumeration specifies the value of the Priority property. Table 21-4 describes the MessagePriority enumeration members.

Table 21-4: The MessagePriority Enumeration Members




Lowest priority


Between Low and Lowest message priority


Low priority


Normal priority


Between High and Normal message priority


High priority


Between Highest and High message priority


Highest priority

The following code creates two messages and sets their priority, body, and label properties:

 Dim msg1 As Message = New Message() Dim msg2 As Message = New Message() msg1.Priority = MessagePriority.High msg1.Body = "Body of first message" msg1.Label = "First Message" msg2.Priority = MessagePriority.Normal msg2.Label = "Second Message" msg2.Body = "Body of second message" 

Applied ADO. NET(c) Building Data-Driven Solutions
Applied ADO.NET: Building Data-Driven Solutions
ISBN: 1590590732
EAN: 2147483647
Year: 2006
Pages: 214 © 2008-2017.
If you may any questions please contact us: