Only the following methods are safe for multithreaded operations: BeginPeek,
Receive. I will try to explain these methods in my articles.
Message Queuing technology allows applications running at different times to communicate across heterogeneous networks and systems which might be temporarily offline. Applications send, receive, or peek (read without removing) messages from queues. Message Queuing is an optional component of Windows 2000 and Windows NT, and must be installed separately.
MessageQueue class is a wrapper around Message Queuing. There are multiple versions of Message Queuing, and using the
MessageQueue class can result in slightly different behavior, depending on the Operating System you are using. For information about specific features of each version of Message Queuing, see the topic "What's New in Message Queuing" in the Platform SDK in MSDN.
MessageQueue class provides a reference to a Message Queuing queue. You can specify a path in the
MessageQueue constructor to connect to an existing resource, or you can create a new queue on the server. Before you can call
Receive, you must associate the new instance of the
MessageQueue class with an existing queue. At that point, you can manipulate the queue properties such as
MessageQueue class supports two types of message retrieval: synchronous and asynchronous. The synchronous methods,
Receive, cause the process thread to wait a specified time interval for a new message to arrive in the queue. The asynchronous methods,
BeginReceive, allow the main application tasks to continue in a separate thread until a message arrives in the queue. These methods work by using callback objects and state objects to communicate information between threads.
When you create a new instance of the
MessageQueue class, you are not creating a new Message Queuing queue. Instead, you can use the
Purge methods to manage queues on the server.
Shared in Visual Basic) members, so you can call them without creating a new instance of the
You can set the
Path property with one of three names: the friendly name, the
FormatName, or the
Label. The friendly name, which is defined by the queue's
QueueName properties, is MachineName\ QueueName for a public queue, and MachineName\ Private$\ QueueName for a private queue. The
FormatName property allows offline access to message queues. Lastly, you can use the queue's
Label property to set the queue's
For a list of initial property values for an instance of
MessageQueue, see the
Using the Message Queue
Create an instance of the class
MessageQueue. Before you can create it, you need to import a reference of the class:
Private WithEvents myQueue As New MessageQueue(".\private$\myQueue")
Private WithEvents myQueue As New MessageQueue()
myQueue.Path = "".\private$\myQueue"
I have declared the message queue with an event because I am going to use some of the events available with the class.
Now, let's see how to send a message to the queue
To send a message to the message queue, you can either create a message using the
Message class or by directly sending a string:
Dim msg As New Message
msg.Label = "Example"
msg.Body = "This message is send as an example"
myQueue.Send("Example","This message is send as an example")
There are some other ways to send messages to the queue. I found these methods over the MSDN. I found some of them very useful.
Public Sub SendPublic()
Dim myQueue As New MessageQueue(".\myQueue")
myQueue.Send("Public queue by path name.")
Public Sub SendPrivate()
Dim myQueue As New MessageQueue(".\Private$\myQueue")
myQueue.Send("Private queue by path name.")
Public Sub SendByLabel()
Dim myQueue As New MessageQueue("Label:TheLabel")
myQueue.Send("Queue by label.")
Public Sub SendByFormatName()
Dim myQueue As New _
myQueue.Send("Queue by format name.")
Now, let's see how to receive message from the queue
By using the method
Receive, we can receive the first message in the queue, removing it from the queue:
But there is a catch: we have to store the message we received in a
Dim tempMsg as New Message
tempMsg = myQueue.Receive()
If you wish to check the message before removing it, you can use the
Dim tempMSg as New Message
tempMsg = MyQueue.Peek()
If tempMsg.Label = "Example" Then
Points of Interest
MessageQueue is useful when there are a couple of applications that share data between them.
I have gone through some examples over the net and MSDN to write this article.