MessageQueue.BeginReceive Method (TimeSpan, Cursor, Object, AsyncCallback)

Note: This method is new in the .NET Framework version 2.0.

Initiates an asynchronous receive operation that has a specified time-out and uses a specified cursor and a specified state object. The state object provides associated information throughout the lifetime of the operation. This overload receives notification, through a callback, of the identity of the event handler for the operation. 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,
	Cursor cursor,
	Object state,
	AsyncCallback callback
public IAsyncResult BeginReceive (
	TimeSpan timeout, 
	Cursor cursor, 
	Object state, 
	AsyncCallback callback
public function BeginReceive (
	timeout : TimeSpan, 
	cursor : Cursor, 
	state : Object, 
	callback : AsyncCallback
) : IAsyncResult



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


A Cursor that maintains a specific position in the message queue.


A state object, specified by the application, that contains information associated with the asynchronous operation.


The AsyncCallback that receives the notification of the asynchronous operation completion.

Return Value

The IAsyncResult that identifies the posted asynchronous request.

Exception typeCondition


The cursor parameter is a null reference (Nothing in Visual Basic).


The value specified for the timeout parameter is not valid.


An error occurred when accessing a Message Queuing method.

When you use this overload, the callback specified in the callback parameter is invoked directly when a message becomes available in the queue or when the specified interval of time has expired; the ReceiveCompleted event is not raised. The other overloads of BeginReceive rely on this component to raise the ReceiveCompleted event.

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 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.

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 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, use the AsyncWaitHandle property of the IAsyncResult to identify the completed operation.

The state object associates state information with the operation. For example, if you call BeginReceive multiple times to initiate multiple operations, you can identify each operation through a separate state object that you define.

You can also use the state object to pass information across process threads. If a thread is started but the callback is on a different thread in an asynchronous scenario, the state object is marshaled and passed back along with information from the event.

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.


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


  • Full trust for the immediate caller. This member cannot be used by partially trusted code. For more information, see .

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0