ReceiveCompletedEventHandler ReceiveCompletedEventHandler ReceiveCompletedEventHandler ReceiveCompletedEventHandler Delegate

定義

代表將處理 ReceiveCompletedMessageQueue 事件的方法。Represents the method that will handle the ReceiveCompleted event of a MessageQueue.

public delegate void ReceiveCompletedEventHandler(System::Object ^ sender, ReceiveCompletedEventArgs ^ e);
[System.Serializable]
public delegate void ReceiveCompletedEventHandler(object sender, ReceiveCompletedEventArgs e);
type ReceiveCompletedEventHandler = delegate of obj * ReceiveCompletedEventArgs -> unit
Public Delegate Sub ReceiveCompletedEventHandler(sender As Object, e As ReceiveCompletedEventArgs)

參數

sender
Object Object Object Object

事件的來源:MessageQueueThe source of the event, the MessageQueue.

繼承
ReceiveCompletedEventHandlerReceiveCompletedEventHandlerReceiveCompletedEventHandlerReceiveCompletedEventHandler
屬性

範例

下列程式碼範例說明如何建立事件處理常式 (ReceiveCompletedEventHandler MyReceiveCompleted) 的事件委派 () MessageQueue.ReceiveCompleted , 並將它與事件產生關聯。The following code example illustrates how to create an event delegate (ReceiveCompletedEventHandler) for the event handler (MyReceiveCompleted) and associate it with the MessageQueue.ReceiveCompleted event. 事件處理常式會從佇列接收訊息, 並將其標籤寫入螢幕。The event handler receives a message from a queue, and writes its label 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

備註

建立 ReceiveCompletedEventHandler 委派時,必須識別處理事件的方法。When you create a ReceiveCompletedEventHandler delegate, you identify the method that will handle the event. 若要使事件與您的事件處理常式產生關聯,請將委派的執行個體 (Instance) 加入至事件。To associate the event with your event handler, add an instance of the delegate to the event. 除非您移除委派,否則每當事件發生時就會呼叫事件處理常式。The event handler is called whenever the event occurs, unless you remove the delegate. 如需事件處理常式委派的詳細資訊, 請參閱處理和引發事件For more information about event-handler delegates, see Handling and Raising Events.

擴充方法

GetMethodInfo(Delegate) GetMethodInfo(Delegate) GetMethodInfo(Delegate) GetMethodInfo(Delegate)

取得表示特定委派所代表之方法的物件。Gets an object that represents the method represented by the specified delegate.

適用於

另請參閱