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!

Receiving Messages Programmatically

You can use a synchronous method called Receive to look at the contents of a queue. When you call Receive on a queue, it 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 peek at the first message on a queue without removing it from the queue. For more information, see Peeking at Messages.

If there are no messages available on the queue when you call Receive, 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 in milliseconds.

When you perform a Receive operation, you may want to temporarily prevent other users from also removing messages from the queue with which you're working. You can do this by setting the DenySharedReceive property on that queue to True. Setting DenySharedReceive prevents any other users from removing messages from the same queue until you release your usage of it, either through garbage collection or by calling Close.

You can also receive messages asynchronously. For more information, see Receiving Messages Asynchronously.

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 details, see Creating MessageQueue Components.
  2. Create an instance of the Message object to hold the retrieved message.
  3. Call the Receive method to remove the message from the queue. Optionally, to specify a time-out for the Receive method, enter the length of time (in milliseconds) that you want the method to wait as an argument of the Receive 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 Receive method, specifying a time-out of 1000 milliseconds.
    MyMessage = mq.Receive(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 Receive method, specifying a timeout of 1000 milliseconds.
    MyMessage = mq.Receive(1000);

See Also

Reading and Receiving Messages | Peeking at Messages | Receiving Messages Asynchronously | Creating MessageQueue Components