ReceiveCompletedEventHandler Delegat
Definicja
Ważne
Niektóre informacje odnoszą się do produktu w wersji wstępnej, który może zostać znacząco zmodyfikowany przed wydaniem. Firma Microsoft nie udziela żadnych gwarancji, jawnych lub domniemanych, w odniesieniu do informacji podanych w tym miejscu.
Reprezentuje metodę, która będzie obsługiwać ReceiveCompleted zdarzenie klasy MessageQueue.
public delegate void ReceiveCompletedEventHandler(System::Object ^ sender, ReceiveCompletedEventArgs ^ e);
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)
Parametry
- sender
- Object
Źródło zdarzenia , MessageQueue.
Element ReceiveCompletedEventArgs zawierający dane zdarzenia.
Przykłady
Poniższy przykład kodu ilustruje sposób tworzenia delegata zdarzeń (ReceiveCompletedEventHandler) dla programu obsługi zdarzeń (MyReceiveCompleted
) i skojarzenia go ze zdarzeniem MessageQueue.ReceiveCompleted . Procedura obsługi zdarzeń odbiera komunikat z kolejki i zapisuje etykietę na ekranie.
#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
Uwagi
Podczas tworzenia delegata ReceiveCompletedEventHandler należy zidentyfikować metodę, która będzie obsługiwać zdarzenie. Aby skojarzyć zdarzenie z programem obsługi zdarzeń, dodaj wystąpienie delegata do zdarzenia. Program obsługi zdarzeń jest wywoływany przy każdym wystąpieniu zdarzenia, o ile nie usunięto delegata. Aby uzyskać więcej informacji na temat delegatów programu obsługi zdarzeń, zobacz Obsługa i podnoszenie zdarzeń.
Metody rozszerzania
GetMethodInfo(Delegate) |
Pobiera obiekt reprezentujący metodę reprezentowaną przez określonego delegata. |