Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

MessageQueue.BeginReceive Method (TimeSpan)

Initiates an asynchronous receive operation that has a specified time-out. The operation is not complete until either a message becomes available in the queue or the time-out occurs.

Namespace:  System.Messaging
Assembly:  System.Messaging (in System.Messaging.dll)
public IAsyncResult BeginReceive(
	TimeSpan timeout
)

Parameters

timeout
Type: System.TimeSpan

A TimeSpan that indicates the interval of time to wait for a message to become available.

Return Value

Type: System.IAsyncResult
The IAsyncResult that identifies the posted asynchronous request.
ExceptionCondition
ArgumentException

The value specified for the timeout parameter is not valid, possibly because it represents a negative number.

MessageQueueException

An error occurred when accessing a Message Queuing method.

In asynchronous processing, you use BeginReceive to raise the ReceiveCompleted event when a message becomes available in the queue or when the specified interval of time has expired.

ReceiveCompleted is also raised if a message already exists in the queue.

To use BeginReceive, create an event handler that processes the results of the asynchronous operation and associate it with your event delegate. BeginReceive initiates an asynchronous receive operation; the MessageQueue is notified, through the raising of the ReceiveCompleted event, when a message arrives in the queue. The MessageQueue can then access the message by calling EndReceive(IAsyncResult) or retrieving the result using the ReceiveCompletedEventArgs.

The BeginReceive method returns immediately, but the asynchronous operation is not completed until the event handler is called.

Because BeginReceive is asynchronous, you can call it to receive a message from the queue without blocking the current thread of execution. To synchronously receive a message, use the Receive method.

Once an asynchronous operation completes, you can call BeginPeek or BeginReceive again in the event handler to keep receiving notifications.

If CanRead is false, the completion event is raised, but an exception will be thrown when calling EndReceive(IAsyncResult).

The IAsyncResult that BeginReceive returns identifies the asynchronous operation that the method started. You can use this IAsyncResult throughout the lifetime of the operation, although you generally do not use it until EndReceive(IAsyncResult) is called. However, if you start several asynchronous operations, you can place their IAsyncResult values in an array and specify whether to wait for all operations or any operation to complete. In this case, you use the AsyncWaitHandle property of the IAsyncResult to identify the completed operation.

This overload specifies a time-out. If the interval specified by the timeout parameter expires, this component raises the ReceiveCompleted event. Because no message exists, a subsequent call to EndReceive(IAsyncResult) will throw an exception.

Do not use the asynchronous call BeginReceive with transactions. If you want to perform a transactional asynchronous operation, call BeginPeek, and put the transaction and the (synchronous) Receive method within the event handler you create for the peek operation. Your event handler might contain functionality as shown in the following C# code.

 myMessageQueue.BeginTransaction();
  myMessageQueue.Receive();
  myMessageQueue.CommitTransaction();

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

Workgroup mode

Available

Local computer

Yes

Local computer and direct format name

Yes

Remote computer

No

Remote computer and direct format name

Yes

The following code example creates an asynchronous receive operation. The code example creates an event handler, MyReceiveCompleted, and attaches it to the ReceiveCompleted event handler delegate. The code example sends a message to a local message queue, then calls BeginReceive(TimeSpan), passing in a time-out value of ten seconds. When a ReceiveCompleted event is raised, the event handler receives the message and writes the message body to the screen.


using System;
using System.Messaging;

public class QueueExample
{
    public static void Main()
    {
        // Create a non-transactional queue on the local computer. 
        // Note that the queue might not be immediately accessible, and 
        // therefore this example might throw an exception of type 
        // System.Messaging.MessageQueueException when trying to send a 
        // message to the newly created queue.
        CreateQueue(".\\exampleQueue", false);

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

        // Add an event handler for the ReceiveCompleted event.
        queue.ReceiveCompleted += new 
                ReceiveCompletedEventHandler(MyReceiveCompleted);

        // Send a message to the queue.
        queue.Send("Example Message");

        // Begin the asynchronous receive operation.
        queue.BeginReceive(TimeSpan.FromSeconds(10.0));

        // Simulate doing other work on the current thread.
        System.Threading.Thread.Sleep(TimeSpan.FromSeconds(10.0));

        return;
    }

    // Creates a new queue. 
    public static void CreateQueue(string queuePath, bool transactional)
    {
        if(!MessageQueue.Exists(queuePath))
        {
            MessageQueue.Create(queuePath, transactional);
        }
        else
        {
            Console.WriteLine(queuePath + " already exists.");
        }
    }

    // Provides an event handler for the ReceiveCompleted event. 
    private static void MyReceiveCompleted(Object source, 
        ReceiveCompletedEventArgs asyncResult)
    {
        // Connect to the queue.
        MessageQueue queue = (MessageQueue)source;

        // End the asynchronous receive operation.
        Message msg = queue.EndReceive(asyncResult.AsyncResult);

        // Display the message information on the screen.
        Console.WriteLine("Message body: {0}", (string)msg.Body);
    }
}

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft. All rights reserved.