ReceiveCompletedEventArgs ReceiveCompletedEventArgs ReceiveCompletedEventArgs ReceiveCompletedEventArgs Class

定義

提供 ReceiveCompleted 事件的資料。Provides data for the ReceiveCompleted event. 當非同步 (Asynchronous) 接收作業呼叫事件處理常式時,會將這個類別的執行個體 (Instance) 傳遞至處理常式。When your asynchronous receive operation calls an event handler, an instance of this class is passed to the handler.

public ref class ReceiveCompletedEventArgs : EventArgs
public class ReceiveCompletedEventArgs : EventArgs
type ReceiveCompletedEventArgs = class
    inherit EventArgs
Public Class ReceiveCompletedEventArgs
Inherits EventArgs
繼承
ReceiveCompletedEventArgsReceiveCompletedEventArgsReceiveCompletedEventArgsReceiveCompletedEventArgs

範例

下列程式碼範例會建立ReceiveCompleted事件的事件處理常式, 並使用將ReceiveCompletedEventHandler它與事件委派產生關聯。The following code example creates an event handler for the ReceiveCompleted event and associates it with the event delegate by using the ReceiveCompletedEventHandler. 事件處理常式MyReceiveCompleted會從佇列接收訊息, 並將其主體寫入畫面。The event handler, MyReceiveCompleted, receives a message from a queue and writes its body to the screen.

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

using namespace System;
using namespace System::Messaging;
ref class MyNewQueue
{
public:

   //*************************************************
   // Provides an event handler for the ReceiveCompleted
   // event.
   //*************************************************
   static void MyReceiveCompleted( Object^ source, ReceiveCompletedEventArgs^ asyncResult )
   {
      // Connect to the queue.
      MessageQueue^ mq = dynamic_cast<MessageQueue^>(source);

      // End the asynchronous Receive operation.
      Message^ m = mq->EndReceive( asyncResult->AsyncResult );

      // Display message information on the screen.
      Console::WriteLine( "Message: {0}", m->Body );

      // Restart the asynchronous Receive operation.
      mq->BeginReceive();
      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();

   // Do other work on the current thread.
   return 0;
}
using System;
using System.Messaging;

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);
            
            // Begin the asynchronous receive operation.
            myQueue.BeginReceive();
            
            // Do other work on the current thread.

            return;
        }


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

            // End the asynchronous Receive operation.
            Message m = mq.EndReceive(asyncResult.AsyncResult);

            // Display message information on the screen.
            Console.WriteLine("Message: " + (string)m.Body);

            // Restart the asynchronous Receive operation.
            mq.BeginReceive();
            
            return; 
        }
    }
}
Imports System.Messaging

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

            ' Begin the asynchronous receive operation.
            myQueue.BeginReceive()

            ' 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)

            ' Connect to the queue.
            Dim mq As MessageQueue = CType([source], MessageQueue)

            ' End the asynchronous Receive operation.
            Dim m As Message = mq.EndReceive(asyncResult.AsyncResult)

            ' Display message information on the screen.
            Console.WriteLine(("Message: " + CStr(m.Body)))

            ' Restart the asynchronous Receive operation.
            mq.BeginReceive()

            Return

        End Sub

End Class

備註

當您使用事件通知, 以非同步方式從佇列接收訊息時, 您必須建立處理訊息處理的方法。When you use event notification to receive messages asynchronously from the queue, you must create a method that handles your message processing. 您的程式碼BeginReceive必須呼叫, 才能開始非同步處理。Your code must call BeginReceive to begin the asynchronous processing. 當收到訊息時, 您的ReceiveCompleted應用程式會透過事件通知。When a message is received, your application is notified through the ReceiveCompleted event. 的實例ReceiveCompletedEventArgs會傳遞至呼叫事件處理常式的事件委派。An instance of ReceiveCompletedEventArgs is passed into the event delegate that calls your event handler. ReceiveCompleted事件相關聯的資料會包含在委派的AsyncResult參數中。The data associated with the ReceiveCompleted event is contained in the delegate's AsyncResult parameter.

有兩種方式可以提供事件完成的通知: 事件通知和回呼。There are two ways to provide notification of event completion: event notification and callbacks. ReceiveCompletedEventArgs只能與事件通知搭配使用。ReceiveCompletedEventArgs is used only with event notification. 如需比較回呼和事件通知的相關資訊, 請參閱事件與回呼「在 MSDN 上」。For information comparing callbacks and event notification, see "Events vs. Callbacks" on MSDN.

ReceiveCompletedEventArgs提供透過Message成員起始非同步接收作業結束之訊息的存取權。ReceiveCompletedEventArgs provides access to the message that initiated the end of the asynchronous receive operation, through the Message member. 這是訊息的替代存取, 其行為與的呼叫MessageQueue.EndReceive大致相同。This is an alternate access to the message, and behaves much the same as a call to MessageQueue.EndReceive.

屬性

AsyncResult AsyncResult AsyncResult AsyncResult

取得或設定要求的非同步作業結果。Gets or sets the result of the asynchronous operation requested.

Message Message Message Message

取得與非同步接收作業關聯的訊息。Gets the message associated with the asynchronous receive operation.

方法

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(Inherited from Object)

適用於

另請參閱