Initiates an asynchronous receive operation that has a specified time-out and a specified state object, which provides associated information throughout the operation's lifetime. The operation is not complete until either a message becomes available in the queue or the time-out occurs.
[Visual Basic]
Overloads Public Function BeginReceive( _
ByVal timeout As TimeSpan, _
ByVal stateObject As Object _
) As IAsyncResult
[C#]
public IAsyncResult BeginReceive(
TimeSpan timeout,
object stateObject
);
[C++]
public: IAsyncResult* BeginReceive(
TimeSpan timeout,
Object* stateObject
);
[JScript]
public function BeginReceive(
timeout : TimeSpan,
stateObject : Object
) : IAsyncResult;
Parameters
- timeout
- A TimeSpan that indicates the interval of time to wait for a message to become available.
- stateObject
- A state object, specified by the application, that contains information associated with the asynchronous operation.
Return Value
The IAsyncResult that identifies the posted asynchronous request.
Exceptions
Remarks
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.
Note ReceiveCompleted is also raised if a message already exists in the queue.
Use this overload to associate information with the operation that will be preserved throughout the operation's lifetime. The event handler can detect this information by looking at the AsyncState property of the IAsyncResult that is associated with the operation.
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.
Note 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, you use the AsyncWaitHandle property of the IAsyncResult to identify the completed operation.
This overload specifies a time-out and a state object. If the interval specified by the timeout parameter expires, this component raises the ReceiveCompleted event, but the IsCompleted property of the operation's associated IAsyncResult is false. Because no message exists, a subsequent call to EndReceive will throw an exception.
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.
Note 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 + direct format name | Yes |
| Remote computer | No |
| Remote computer + direct format name | Yes |
Requirements
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family
.NET Framework Security:
See Also
MessageQueue Class | MessageQueue Members | System.Messaging Namespace | MessageQueue.BeginReceive Overload List | EndReceive | ReceiveCompleted | BeginPeek | Receive | Peek | TimeSpan