You can use the Peek method to look at the first message on any queue without removing that message from the queue. This allows your component to get information from the queue without preventing other applications or components from retrieving messages they were intended to process.
Note Peek allows you to see only the first message on the queue. Because that message is not removed from the queue when you peek at it, you cannot then peek at subsequent messages. If you want to see all of the messages in a queue without removing them from the queue, you can use the GetMessages method or the GetMessagesEnumerator method. For more information, see Queue and Message Collections.
If there are no messages in the queue when you call Peek, the method waits until a message arrives. You can specify a time-out period if you want the method to wait only a specified period of time. The time-out period is specified in milliseconds. Most commonly, the time-out period will be set to either zero, in which case it checks for a message and does not wait at all, or to the default infinite setting, which waits indefinitely. You would set this in code using the following syntax:
[Visual Basic]
Message.TimeToBeReceived = Message.Infinite
[C#]
Message.TimeToBeReceived = Message.Infinite;
Peeking can be synchronous or asynchronous. For more information, see Peeking at Messages Asynchronously.
To peek at messages synchronously
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 Peek method, specifying a time-out of 1000 milliseconds. MyMessage = mq.Peek(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 Peek method, specifying a timeout of 1000 milliseconds. MyMessage = mq.Peek(1000);
Reading and Receiving Messages | Receiving Messages Programmatically | Creating MessageQueue Components