Get the Message - MSMQ

Recieving a Message

On the receiving side of the message queue must be an application that takes the incoming message and processes it. The code below illustrates how to accomplish this task.

Private Sub ReceiveMessage()

Dim QI as New MSMQQueueInfo
Dim ReceiveQ As MSMQQueue
Dim msg As MSMQMessage

QI.PathName = "MyServer\QueueName"

Set msg = ReceiveQ.Receive(ReceiveTimeout:=1000)
If msg is Nothing Then
MsgBox "No message was received"
' Code to process the message goes here
End If


End Sub

The only significant difference between this example and the one used to send a message is the Receive method. It takes a parameter that indicates how long the function should wait if no message exists in the message queue. In this example, we are waiting for 1000 milliseconds or one second. If the function returns a valid MSMQMessage object, then we can process the body as we wish. Otherwise, the function returns a null reference (identified by the 'Is Nothing' check). A ReceiveTimeout value of 0 means that no waiting will occur. A value of -1 means that the function will block until a message is received.

You might also like...


About the author

Bruce Johnson Canada

I am the owner of a small application development consulting company that specialized in the design and implementation of Internet-based applications. While there are others who can make a web ...

Interested in writing for us? Find out more.


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“An idiot with a computer is a faster, better idiot” - Rich Julius