MessageQueue.PeekById Method (String, TimeSpan)


Peeks the message whose message identifier matches the id parameter. Waits until the message appears in the queue or a time-out occurs.

Namespace:   System.Messaging
Assembly:  System.Messaging (in System.Messaging.dll)

public Message PeekById(
	string id,
	TimeSpan timeout


Type: System.String

The Id of the message to peek.

Type: System.TimeSpan

A TimeSpan that indicates the time to wait until a new message is available for inspection.

Return Value

Type: System.Messaging.Message

The Message whose Id property matches the id parameter.

Exception Condition

The id parameter is null.


The value specified for the timeout parameter is not valid, possibly timeout is less than TimeSpan.Zero or greater than MessageQueue.InfiniteTimeout.


The message with the specified id does not exist in the queue and did not arrive before the period specified by the timeout parameter expired.


An error occurred when accessing a Message Queuing method.

Use PeekById(String) to read, without removing from the queue, a message that has a known message identifier. The identifier of a message is unique across the Message Queuing enterprise, so there will be at most one message in the queue that matches the given id parameter. This overload throws an exception if the queue does not currently contain the message and a new message does not arrive before the time-out occurs.

The timeout parameter does not specify the total running time for this method. Rather, it specifies the time to wait for a new message to arrive in the queue. Each time a new message arrives, this method examines the Id of the new message to see if it matches the id parameter. If not, this method starts the time-out period over and waits for another new message to arrive. Therefore, if new messages continue to arrive within the time-out period, it is possible for this method to continue running indefinitely, either until the time-out period expires without any new messages arriving, or until a message arrives whose Id matches the id parameter.

Two additional methods allow you to peek messages in a queue: Peek and PeekByCorrelationId(String). The Peek method returns the first message in the queue; PeekByCorrelationId(String) returns an acknowledgment, report, or application-generated response message that was created as a result of a message sent to the queue.

The following table shows whether this method is available in various Workgroup modes.

Workgroup mode


Local computer


Local computer and direct format name


Remote computer


Remote computer and direct format name


The following code example demonstrates the use of PeekById(String, TimeSpan).

// Connect to a queue on the local computer.
MessageQueue queue = new MessageQueue(".\\exampleQueue");

// Create a new message.
Message msg = new Message("Example Message Body");

// Send the message.
queue.Send(msg, "Example Message Label");

// Get the message's Id property value.
string id = msg.Id;

// Peek at the message.
msg = queue.PeekById(id, TimeSpan.FromSeconds(10.0));

.NET Framework
Available since 1.1
Return to top