ReceiveCompletedEventArgs 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
为 ReceiveCompleted 事件提供数据。 当异步接收操作调用事件处理程序时,此类的一个实例将被传递给该处理程序。
public ref class ReceiveCompletedEventArgs : EventArgs
public class ReceiveCompletedEventArgs : EventArgs
type ReceiveCompletedEventArgs = class
inherit EventArgs
Public Class ReceiveCompletedEventArgs
Inherits EventArgs
- 继承
示例
下面的代码示例为 事件创建事件处理程序, ReceiveCompleted 并使用 ReceiveCompletedEventHandler将其与事件委托相关联。 事件处理程序 MyReceiveCompleted
从队列接收消息,并将其正文写入屏幕。
#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 应用程序。 的 ReceiveCompletedEventArgs 实例将传递到调用事件处理程序的事件委托中。 与 ReceiveCompleted 事件关联的数据包含在委托的 AsyncResult 参数中。
可通过两种方式提供事件完成通知:事件通知和回调。 ReceiveCompletedEventArgs 仅用于事件通知。 有关比较回调和事件通知的信息,请参阅 MSDN 上的“事件与回调”。
ReceiveCompletedEventArgs 通过 Message 成员提供对启动异步接收操作结束的消息的访问。 这是对消息的备用访问,其行为与对 的调用 MessageQueue.EndReceive大致相同。
属性
AsyncResult |
获取或设置请求的异步操作结果。 |
Message |
获取同异步接收操作关联的消息。 |
方法
Equals(Object) |
确定指定对象是否等于当前对象。 (继承自 Object) |
GetHashCode() |
作为默认哈希函数。 (继承自 Object) |
GetType() |
获取当前实例的 Type。 (继承自 Object) |
MemberwiseClone() |
创建当前 Object 的浅表副本。 (继承自 Object) |
ToString() |
返回表示当前对象的字符串。 (继承自 Object) |