MessageQueue.BeginReceive Method
Initiates an asynchronous receive operation by telling Message Queuing to begin receiving a message and notify the event handler when finished.
Overload List
Initiates an asynchronous receive operation that has no time-out. The operation is not complete until a message becomes available in the queue.
[Visual Basic] Overloads Public Function BeginReceive() As IAsyncResult
[C#] public IAsyncResult BeginReceive();
[C++] public: IAsyncResult* BeginReceive();
[JScript] public function BeginReceive() : IAsyncResult;
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.
[Visual Basic] Overloads Public Function BeginReceive(TimeSpan) As IAsyncResult
[C#] public IAsyncResult BeginReceive(TimeSpan);
[C++] public: IAsyncResult* BeginReceive(TimeSpan);
[JScript] public function BeginReceive(TimeSpan) : IAsyncResult;
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(TimeSpan, Object) As IAsyncResult
[C#] public IAsyncResult BeginReceive(TimeSpan, object);
[C++] public: IAsyncResult* BeginReceive(TimeSpan, Object*);
[JScript] public function BeginReceive(TimeSpan, Object) : IAsyncResult;
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. 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.
[Visual Basic] Overloads Public Function BeginReceive(TimeSpan, Object, AsyncCallback) As IAsyncResult
[C#] public IAsyncResult BeginReceive(TimeSpan, object, AsyncCallback);
[C++] public: IAsyncResult* BeginReceive(TimeSpan, Object*, AsyncCallback*);
[JScript] public function BeginReceive(TimeSpan, Object, AsyncCallback) : IAsyncResult;
Example
[Visual Basic, C#, C++] The following example chains asynchronous requests. It assumes there is a queue on the local computer called "myQueue". The Main function begins the asynchronous operation that is handled by the MyReceiveCompleted routine. MyReceiveCompleted processes the current message and begins a new asynchronous receive operation.
[Visual Basic, C#, C++] Note This example shows how to use one of the overloaded versions of BeginReceive. For other examples that might be available, see the individual overload topics.
[Visual Basic] Imports System Imports System.Messaging Imports System.Threading Namespace MyProject '/ <summary> '/ Provides a container class for the example. '/ </summary> Public Class MyNewQueue ' Define static class members. Private Shared signal As New ManualResetEvent(False) Private Shared count As Integer = 0 '************************************************** ' Provides an entry point into the application. ' ' This example performs asynchronous receive ' operation processing. '************************************************** Public Shared Sub Main() ' Create an instance of MessageQueue. Set its formatter. Dim myQueue As New MessageQueue(".\myQueue") myQueue.Formatter = New XmlMessageFormatter(New Type() _ {GetType([String])}) ' Add an event handler for the ReceiveCompleted event. AddHandler myQueue.ReceiveCompleted, AddressOf _ MyReceiveCompleted ' Begin the asynchronous receive operation. myQueue.BeginReceive() signal.WaitOne() ' Do other work on the current thread. Return End Sub 'Main '*************************************************** ' Provides an event handler for the ReceiveCompleted ' event. '*************************************************** Private Shared Sub MyReceiveCompleted(ByVal [source] As _ [Object], ByVal asyncResult As ReceiveCompletedEventArgs) Try ' Connect to the queue. Dim mq As MessageQueue = CType([source], MessageQueue) ' End the asynchronous receive operation. Dim m As Message = _ mq.EndReceive(asyncResult.AsyncResult) count += 1 If count = 10 Then signal.Set() End If ' Restart the asynchronous receive operation. mq.BeginReceive() Catch ' Handle sources of MessageQueueException. ' Handle other exceptions. End Try Return End Sub 'MyReceiveCompleted End Class 'MyNewQueue End Namespace 'MyProject [C#] using System; using System.Messaging; using System.Threading; namespace MyProject { /// <summary> /// Provides a container class for the example. /// </summary> public class MyNewQueue { // Define static class members. static ManualResetEvent signal = new ManualResetEvent(false); static int count = 0; //************************************************** // Provides an entry point into the application. // // This example performs asynchronous receive // operation processing. //************************************************** public static void Main() { // Create an instance of MessageQueue. Set its formatter. MessageQueue myQueue = new MessageQueue(".\\myQueue"); myQueue.Formatter = new XmlMessageFormatter(new Type[] {typeof(String)}); // Add an event handler for the ReceiveCompleted event. myQueue.ReceiveCompleted += new ReceiveCompletedEventHandler(MyReceiveCompleted); // Begin the asynchronous receive operation. myQueue.BeginReceive(); signal.WaitOne(); // Do other work on the current thread. return; } //*************************************************** // Provides an event handler for the ReceiveCompleted // event. //*************************************************** private static void MyReceiveCompleted(Object source, ReceiveCompletedEventArgs asyncResult) { try { // Connect to the queue. MessageQueue mq = (MessageQueue)source; // End the asynchronous receive operation. Message m = mq.EndReceive(asyncResult.AsyncResult); count += 1; if (count == 10) { signal.Set(); } // Restart the asynchronous receive operation. mq.BeginReceive(); } catch(MessageQueueException) { // Handle sources of MessageQueueException. } // Handle other exceptions. return; } } } [C++] #using <mscorlib.dll> #using <system.dll> #using <system.messaging.dll> using namespace System; using namespace System::Messaging; using namespace System::Threading; __gc class MyNewQueue { public: // Define static class members. static ManualResetEvent* signal = new ManualResetEvent(false); static int count = 0; // Provides an event handler for the ReceiveCompleted // event. public: static void MyReceiveCompleted(Object* source, ReceiveCompletedEventArgs* asyncResult) { try { // Connect to the queue. MessageQueue* mq = dynamic_cast<MessageQueue*>(source); // End the asynchronous receive operation. mq->EndReceive(asyncResult->AsyncResult); count += 1; if (count == 10) { signal->Set(); } // Restart the asynchronous receive operation. mq->BeginReceive(); } catch (MessageQueueException*) { // Handle sources of MessageQueueException. } // Handle other exceptions. return; } }; // Provides an entry point into the application. // // This example performs asynchronous receive // operation processing. int main() { // Create an instance of MessageQueue. Set its formatter. MessageQueue* myQueue = new MessageQueue(S".\\myQueue"); Type* p __gc[] = new Type* __gc[1]; p[0] = __typeof(String); myQueue->Formatter = new XmlMessageFormatter( p ); // Add an event handler for the ReceiveCompleted event. myQueue->ReceiveCompleted += new ReceiveCompletedEventHandler(0, MyNewQueue::MyReceiveCompleted); // Begin the asynchronous receive operation. myQueue->BeginReceive(); MyNewQueue::signal->WaitOne(); // Do other work on the current thread. return 0; }
[Visual Basic, C#, C++] The following example queues asynchronous requests. The call to BeginReceive uses the AsyncWaitHandle in its return value. The Main routine waits for all asynchronous operations to be completed before exiting.
[Visual Basic] Imports System Imports System.Messaging Imports System.Threading Namespace MyProject '/ <summary> '/ Provides a container class for the example. '/ </summary> Public Class MyNewQueue '************************************************** ' Provides an entry point into the application. ' ' This example performs asynchronous receive ' operation processing. '************************************************** Public Shared Sub Main() ' Create an instance of MessageQueue. Set its formatter. Dim myQueue As New MessageQueue(".\myQueue") myQueue.Formatter = New XmlMessageFormatter(New Type() _ {GetType([String])}) ' Add an event handler for the ReceiveCompleted event. AddHandler myQueue.ReceiveCompleted, AddressOf _ MyReceiveCompleted ' Define wait handles for multiple operations. Dim waitHandleArray(10) As WaitHandle Dim i As Integer For i = 0 To 9 ' Begin asynchronous operations. waitHandleArray(i) = _ myQueue.BeginReceive().AsyncWaitHandle Next i ' Specify to wait for all operations to return. WaitHandle.WaitAll(waitHandleArray) Return End Sub 'Main '*************************************************** ' Provides an event handler for the ReceiveCompleted ' event. '*************************************************** Private Shared Sub MyReceiveCompleted(ByVal [source] As _ [Object], ByVal asyncResult As ReceiveCompletedEventArgs) Try ' Connect to the queue. Dim mq As MessageQueue = CType([source], MessageQueue) ' End the asynchronous receive operation. Dim m As Message = _ mq.EndReceive(asyncResult.AsyncResult) ' Process the message here. Console.WriteLine("Message received.") Catch ' Handle sources of MessageQueueException. ' Handle other exceptions. End Try Return End Sub 'MyReceiveCompleted End Class 'MyNewQueue End Namespace 'MyProject [C#] using System; using System.Messaging; using System.Threading; namespace MyProject { /// <summary> /// Provides a container class for the example. /// </summary> public class MyNewQueue { //************************************************** // Provides an entry point into the application. // // This example performs asynchronous receive // operation processing. //************************************************** public static void Main() { // Create an instance of MessageQueue. Set its formatter. MessageQueue myQueue = new MessageQueue(".\\myQueue"); myQueue.Formatter = new XmlMessageFormatter(new Type[] {typeof(String)}); // Add an event handler for the ReceiveCompleted event. myQueue.ReceiveCompleted += new ReceiveCompletedEventHandler(MyReceiveCompleted); // Define wait handles for multiple operations. WaitHandle[] waitHandleArray = new WaitHandle[10]; for(int i=0; i<10; i++) { // Begin asynchronous operations. waitHandleArray[i] = myQueue.BeginReceive().AsyncWaitHandle; } // Specify to wait for all operations to return. WaitHandle.WaitAll(waitHandleArray); return; } //*************************************************** // Provides an event handler for the ReceiveCompleted // event. //*************************************************** private static void MyReceiveCompleted(Object source, ReceiveCompletedEventArgs asyncResult) { try { // Connect to the queue. MessageQueue mq = (MessageQueue)source; // End the asynchronous receive operation. Message m = mq.EndReceive(asyncResult.AsyncResult); // Process the message here. Console.WriteLine("Message received."); } catch(MessageQueueException) { // Handle sources of MessageQueueException. } // Handle other exceptions. return; } } } [C++] #using <mscorlib.dll> #using <system.dll> #using <system.messaging.dll> using namespace System; using namespace System::Messaging; using namespace System::Threading; __gc class MyNewQueue { // Provides an event handler for the ReceiveCompleted // event. public: static void MyReceiveCompleted(Object* source, ReceiveCompletedEventArgs* asyncResult) { try { // Connect to the queue. MessageQueue* mq = dynamic_cast<MessageQueue*>(source); // End the asynchronous receive operation. mq->EndReceive(asyncResult->AsyncResult); // Process the message here. Console::WriteLine(S"Message received."); } catch (MessageQueueException*) { // Handle sources of MessageQueueException. } // Handle other exceptions. return; } }; // Provides an entry point into the application. // // This example performs asynchronous receive // operation processing. int main() { // Create an instance of MessageQueue. Set its formatter. MessageQueue* myQueue = new MessageQueue(S".\\myQueue"); Type* p __gc[] = new Type* __gc[1]; p[0] = __typeof(String); myQueue->Formatter = new XmlMessageFormatter( p ); // Add an event handler for the ReceiveCompleted event. myQueue->ReceiveCompleted += new ReceiveCompletedEventHandler(0, MyNewQueue::MyReceiveCompleted); // Define wait handles for multiple operations. WaitHandle* waitHandleArray[] = new WaitHandle*[10]; for (int i=0; i<10; i++) { // Begin asynchronous operations. waitHandleArray->Item[i] = myQueue->BeginReceive()->AsyncWaitHandle; } // Specify to wait for all operations to return. WaitHandle::WaitAll(waitHandleArray); return 0; }
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
See Also
MessageQueue Class | MessageQueue Members | System.Messaging Namespace