You can use a synchronous method called Receive to look at the contents of a queue. When you call Receive on a queue, it removes the first message from the queue and returns it to you. This message is no longer available to other components looking at the queue.
Note You can also peek at the first message on a queue without removing it from the queue. For more information, see Peeking at Messages.
If there are no messages available on the queue when you call Receive, the method will wait until a message arrives on the queue. You can specify a time-out period if you want the method to wait for only a specified interval. The time-out period is specified in milliseconds.
When you perform a Receive operation, you may want to temporarily prevent other users from also removing messages from the queue with which you're working. You can do this by setting the DenySharedReceive property on that queue to True. Setting DenySharedReceive prevents any other users from removing messages from the same queue until you release your usage of it, either through garbage collection or by calling Close.
You can also receive messages asynchronously. For more information, see Receiving Messages Asynchronously.
To receive a message programmatically
When you're finished, your code might look like this:
[Visual Basic] 'Create a connection to the queue you want to communicate with. Dim mq as new MessageQueue("YourMachine\YourQueue") 'Create a Message object. Dim MyMessage as Message 'Call the Receive method, specifying a time-out of 1000 milliseconds. MyMessage = mq.Receive(1000) [C#] // Create a connection to the queue you want to communicate with. MessageQueue mq = new MessageQueue(@"YourMachine\YourQueue"); // Create a Message object. Message MyMessage; // Call the Receive method, specifying a timeout of 1000 milliseconds. MyMessage = mq.Receive(1000);
Reading and Receiving Messages | Peeking at Messages | Receiving Messages Asynchronously | Creating MessageQueue Components