This documentation is archived and is not being maintained.

Receiving Messages Programmatically

You can use a synchronous method called Receive to look at the contents of a queue. When you call the Receive method on a queue, the system removes the first message from the queue and returns it to you. This message is no longer available to other components looking at the queue.

Note   You can also look at the first message on a queue without removing it from the queue. For more information, see Peeking at Messages. You can also receive messages asynchronously. For more information, see Receiving Messages Asynchronously.

If there are no messages available on the queue when you call the Receive method, the method will wait until a message arrives on the queue. You can specify a time-out period if you want the method to wait for only a specified interval. The time-out period is specified as a TimeSpan object.

When you read messages from a queue, a formatter object is used to serialize and deserialize the message's contents as you manipulate the message. For more information, see Introduction to Reading and Retrieving Messages.

There are several forms of the Receive method — the basic method with several overloads that allow you to specify things such as time-out periods, a method called MessageQueue.ReceiveById that allows you to retrieve a particular message by its identifier, and a method called ReceiveByCorrelationID that allows you to retrieve a message by correlation ID. For an explanation of correlation IDs, see Message Queue Journal Storage.

To receive a message programmatically

  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 Creating MessageQueue Component Instances.
  2. Specify the formatter to use to retrieve your message. For more information, see Specifying the Formatter for Retrieved Messages.
  3. Create an instance of the Message object to hold the retrieved message.
  4. Call the Receive method to remove the message from the queue.
    Tip   To specify a time-out for the Receive method, use a TimeSpan object to specify the length of time that you want the method to wait.

    When you are finished, your code might look like this:

    ' Visual Basic
    Dim mq As New System.Messaging.MessageQueue(".\MyQueue")
    mq.Send("1", "1")
    Dim m As System.Messaging.Message
    m = mq.Receive(New TimeSpan(0, 0, 3))
    m.Formatter = New _
       System.Messaging.XmlMessageFormatter(New String() 
    // C#
    System.Messaging.MessageQueue mq = 
       new System.Messaging.MessageQueue(".\\MyQueue");
    mq.Send("1", "1");
    System.Messaging.Message m = mq.Receive(new TimeSpan(0,0,3));
    m.Formatter = new System.Messaging.XmlMessageFormatter(new string[] 

See Also

Reading and Receiving Messages | Peeking at Messages | Receiving Messages Asynchronously | Creating MessageQueue Component Instances | Specifying the Formatter for Retrieved Messages