MessageQueue.ReceiveCompleted 事件
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
在从队列移除某条消息后发生。 此事件由异步操作 BeginReceive() 引发。
public:
event System::Messaging::ReceiveCompletedEventHandler ^ ReceiveCompleted;
[System.Messaging.MessagingDescription("MQ_ReceiveCompleted")]
public event System.Messaging.ReceiveCompletedEventHandler ReceiveCompleted;
[<System.Messaging.MessagingDescription("MQ_ReceiveCompleted")>]
member this.ReceiveCompleted : System.Messaging.ReceiveCompletedEventHandler
Public Custom Event ReceiveCompleted As ReceiveCompletedEventHandler
事件类型
- 属性
示例
下面的代码示例创建名为 的 MyReceiveCompleted
事件处理程序,将其 ReceiveCompleted 附加到事件处理程序委托,并调用 BeginReceive 以对位于路径“.\myQueue”的队列启动异步接收操作。 当引发事件 ReceiveCompleted 时,该示例接收消息并将其正文写入屏幕。 然后,该示例再次调用 BeginReceive 以启动新的异步接收操作。
#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
注解
BeginReceive 在异步处理中使用 ,以在 ReceiveCompleted 队列中提供消息时引发 事件。
EndReceive(IAsyncResult) 用于完成由调用 BeginReceive 启动的操作,并在引发事件时 ReceiveCompleted 查看消息。
创建 ReceiveCompletedEventHandler 委托时,需要标识将处理该事件的方法。 若要将事件与事件处理程序关联,请将该委托的一个实例添加到事件中。 除非移除了该委托,否则每当发生该事件时就会调用事件处理程序。 有关事件处理程序委托的详细信息,请参阅 处理和引发事件。