NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

Peeking at Messages

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

  1. Create an instance of the MessageQueue component and set its Path property to the queue to which you want to refer. For details, see Creating MessageQueue Components.
  2. Create an instance of the Message object to hold the data the Peek method copies from the queue.
  3. Call the Peek method to get data about the first message on the queue. Optionally, to specify a time-out for the Peek method, enter the length of time (in milliseconds) that you want the method to wait as an argument of the method.

    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);

See Also

Reading and Receiving Messages | Receiving Messages Programmatically | Creating MessageQueue Components