MessageQueue.EndReceive(IAsyncResult) MessageQueue.EndReceive(IAsyncResult) MessageQueue.EndReceive(IAsyncResult) MessageQueue.EndReceive(IAsyncResult) Method

定義

完成指定的非同步接收作業。Completes the specified asynchronous receive operation.

public:
 System::Messaging::Message ^ EndReceive(IAsyncResult ^ asyncResult);
public System.Messaging.Message EndReceive (IAsyncResult asyncResult);
member this.EndReceive : IAsyncResult -> System.Messaging.Message
Public Function EndReceive (asyncResult As IAsyncResult) As Message

參數

asyncResult
IAsyncResult IAsyncResult IAsyncResult IAsyncResult

IAsyncResult,識別要完成的非同步接收作業,並要從其中擷取最終結果。The IAsyncResult that identifies the asynchronous receive operation to finish and from which to retrieve an end result.

傳回

與完成的非同步作業相關的 MessageThe Message associated with the completed asynchronous operation.

例外狀況

asyncResult 參數為 nullThe asyncResult parameter is null.

asyncResult 參數的語法無效。The syntax of the asyncResult parameter is not valid.

存取訊息佇列方法時發生錯誤。An error occurred when accessing a Message Queuing method.

範例

下列程式碼範例會連結非同步要求。The following code example chains asynchronous requests. 它假設本機電腦上有一個稱為 "myQueue" 的佇列。It assumes there is a queue on the local computer called "myQueue". 函式會開始MyReceiveCompleted常式所處理的非同步作業。 MainThe Main function begins the asynchronous operation that is handled by the MyReceiveCompleted routine. MyReceiveCompleted處理目前的訊息, 並開始新的非同步接收作業。MyReceiveCompleted processes the current message and begins a new asynchronous receive operation.

#using <system.dll>
#using <system.messaging.dll>

using namespace System;
using namespace System::Messaging;
using namespace System::Threading;

ref class MyNewQueue
{
public:

   // Define static class members.
   static ManualResetEvent^ signal = gcnew ManualResetEvent( false );
   static int count = 0;

   // Provides an event handler for the ReceiveCompleted
   // event.
   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 = gcnew MessageQueue( ".\\myQueue" );
   array<Type^>^p = gcnew array<Type^>(1);
   p[ 0 ] = String::typeid;
   myQueue->Formatter = gcnew XmlMessageFormatter( p );

   // Add an event handler for the ReceiveCompleted event.
   myQueue->ReceiveCompleted += gcnew ReceiveCompletedEventHandler( MyNewQueue::MyReceiveCompleted );

   // Begin the asynchronous receive operation.
   myQueue->BeginReceive();
   MyNewQueue::signal->WaitOne();

   // Do other work on the current thread.
   return 0;
}
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; 
        }
    }
}
Imports System.Messaging
Imports System.Threading




' Provides a container class for the example.

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



        ' 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

End Class

備註

當引發EndReceive(IAsyncResult)事件時, 會完成BeginReceive呼叫所起始的作業。 ReceiveCompletedWhen the ReceiveCompleted event is raised, EndReceive(IAsyncResult) completes the operation that was initiated by the BeginReceive call. 若要這樣做EndReceive(IAsyncResult) , 會接收訊息。To do so, EndReceive(IAsyncResult) receives the message.

BeginReceive可以指定超時時間, 如果在佇列中出現ReceiveCompleted訊息之前發生超時, 則會引發事件。BeginReceive can specify a time-out, which causes the ReceiveCompleted event to be raised if the time-out occurs before a message appears in the queue. 當發生超時而沒有訊息抵達佇列時, 後續的呼叫EndReceive(IAsyncResult)會擲回例外狀況。When a time-out occurs without a message arriving in the queue, a subsequent call to EndReceive(IAsyncResult) throws an exception.

EndReceive(IAsyncResult)是用來讀取 (從佇列中移除) 導致ReceiveCompleted引發事件的訊息。EndReceive(IAsyncResult) is used to read (removing from the queue) the message that caused the ReceiveCompleted event to be raised.

如果您想要繼續以非同步方式接收訊息, 您可以在BeginReceive呼叫EndReceive(IAsyncResult)之後再次呼叫。If you want to continue to asynchronously receive messages, you can again call BeginReceive after calling EndReceive(IAsyncResult).

下表顯示這個方法是否可在各種工作組模式中使用。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

適用於

另請參閱