Returns the first message in the queue without removing it from the queue.
Returns without removing (peeks) the first message available in the queue referenced by the MessageQueue. This call is synchronous. It blocks the current thread of execution until a message is available.
[Visual Basic] Overloads Overridable Public Function Peek() As Message
[C#] public virtual Message Peek();
[C++] public: virtual Message* Peek();
[JScript] public function Peek() : Message;
Returns without removing (peeks) the first message available in the queue referenced by the MessageQueue. Waits the specified interval for a message to become available.
[Visual Basic] Overloads Overridable Public Function Peek(TimeSpan) As Message
[C#] public virtual Message Peek(TimeSpan);
[C++] public: virtual Message* Peek(TimeSpan);
[JScript] public function Peek(TimeSpan) : Message;
The following example uses the Peek and Receive methods on a queue. A Message object is instantiated to refer to the first message in the queue. If the first message is an application-defined marker, the queue is purged. Otherwise, the first message is read and removed.
This procedure assumes that the queue "myQueue" exists on the computer "myComputer". It also assumes that the message body "Purge the queue" is used to indicate that the queue should be purged.
Note This example shows how to use one of the overloaded versions of Peek. For other examples that may be available, see the individual overload topics.
[Visual Basic]
Private Sub PeekQueue() 'Define a Message. Dim msg As System.Messaging.Message 'Define a string to hold the contents of the message body Dim msgBody As String 'Define a new MessageQueue. Dim messageQueue1 As MessageQueue 'Associate the MessageQueue with the given queue. messageQueue1 = New MessageQueue("myComputer\myQueue") 'Read without removing (peek) the first message in the queue. msg = messageQueue1.Peek 'If the message body indicates a specific marker, purge the queue. If msg.BodyStream.ToString = "Purge the queue." Then messageQueue1.Purge() Else 'Otherwise, remove the top message from the queue (receive it). msg = MessageQueue1.Receive End If End Sub
MessageQueue Class | MessageQueue Members | System.Messaging Namespace