How to: Peek at Messages

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

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 GetAllMessages method or the GetMessageEnumerator method. For more information, see Queue and Message Collections.

If there are no messages in the queue when you call the Peek method, 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 as a TimeSpan object. 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:

msg.TimeToBeReceived = System.Messaging.Message.InfiniteTimeout
        msg.TimeToBeReceived = System.Messaging.Message.InfiniteTimeout;
msg.set_TimeToBeReceived(System.Messaging.Message.InfiniteTimeout);

You can also set this in the Properties window by setting the TimeToBeReceived property.

Tip

When using dependent clients, be sure the clock on the client computer is synchronized with the clock on the server that is running Message Queuing. Otherwise, unpredictable behavior might result when sending a message whose TimeToBeReceived property is not InfiniteTimeout .

Peeking can be synchronous or asynchronous. For more information, see How to: Receive 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 more information, see How to: Create MessageQueue Component Instances.

  2. Create an instance of the Message object to hold the data that 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 (as a TimeSpan object) that you want the method to wait as an argument of the method.

    For example, the following code shows how you can use the Peek method to return and display information about the first message on a queue.

    Public Sub LookFirstMessage()
        Dim NewQueue As New System.Messaging.MessageQueue(".\MyQueue")
        Dim FirstMessage As System.Messaging.Message
        FirstMessage = NewQueue.Peek
        Dim targetNames = {"System.String,mscorlib"}
        FirstMessage.Formatter =
           New System.Messaging.XmlMessageFormatter(targetNames)
        Dim label = CStr(FirstMessage.Label)
    End Sub
    
        public void LookFirstMessage()
        {
            System.Messaging.MessageQueue newQueue =
               new System.Messaging.MessageQueue(".\\MyQueue");
            System.Messaging.Message firstMessage;
            string[] types = { "System.String,mscorlib" };
            firstMessage = newQueue.Peek();
            firstMessage.Formatter =
               new System.Messaging.XmlMessageFormatter(types);
            string label = firstMessage.Label.ToString();
        }
    

See Also

Tasks

How to: Receive Messages Programmatically

How to: Create MessageQueue Component Instances

Other Resources

Reading and Receiving Messages