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 ReceiveQ = QI.Open(MQ_RECEIVE_ACCESS, MQ_DENY_NONE)
Set msg = ReceiveQ.Receive(ReceiveTimeout:=1000)
If msg is Nothing Then
MsgBox "No message was received"
Else
' Code to process the message goes here
End If
ReceiveQ.Close
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.
Comments