PeekCompletedEventArgs 类

定义

PeekCompleted 事件提供数据。Provides data for the PeekCompleted event. 当异步查看操作调用事件处理程序时,此类的一个实例将被传递给该处理程序。When your asynchronous peek operation calls an event handler, an instance of this class is passed to the handler.

public ref class PeekCompletedEventArgs : EventArgs
public class PeekCompletedEventArgs : EventArgs
type PeekCompletedEventArgs = class
    inherit EventArgs
Public Class PeekCompletedEventArgs
Inherits EventArgs
继承
PeekCompletedEventArgs

示例

下面的代码示例为PeekCompleted事件创建事件处理程序, 并使用将PeekCompletedEventHandler其与事件委托相关联。The following code example creates an event handler for the PeekCompleted event and associates it with the event delegate by using the PeekCompletedEventHandler. 事件处理程序MyPeekCompleted查看消息并将其标签写入屏幕。The event handler, MyPeekCompleted, peeks a message and writes its label to the screen.

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

using namespace System;
using namespace System::Messaging;

// This example performs asynchronous peek operation
// processing.
//*************************************************
ref class MyNewQueue
{
public:

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

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

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

      // Restart the asynchronous peek operation.
      mq->BeginPeek();
      return;
   }
};

// Provides an entry point into the application.
//         
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 PeekCompleted event.
   myQueue->PeekCompleted += gcnew PeekCompletedEventHandler( MyNewQueue::MyPeekCompleted );

   // Begin the asynchronous peek operation.
   myQueue->BeginPeek();

   // 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 peek 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 PeekCompleted event.
            myQueue.PeekCompleted += new 
                PeekCompletedEventHandler(MyPeekCompleted);
            
            // Begin the asynchronous peek operation.
            myQueue.BeginPeek();
            
            // Do other work on the current thread.

            return;
        }


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

            // End the asynchronous peek operation.
            Message m = mq.EndPeek(asyncResult.AsyncResult);

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

            // Restart the asynchronous peek operation.
            mq.BeginPeek();
            
            return; 
        }
    }
}
Imports System.Messaging





' Provides a container class for the example.
Public Class MyNewQueue



        ' Provides an entry point into the application.
        '		 
        ' This example performs asynchronous peek 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 PeekCompleted event.
            AddHandler myQueue.PeekCompleted, AddressOf _
                MyPeekCompleted

            ' Begin the asynchronous peek operation.
            myQueue.BeginPeek()

            ' Do other work on the current thread.
            Return
        End Sub


        '**************************************************
        ' Provides an event handler for the PeekCompleted
        ' event.
        '**************************************************

        Private Shared Sub MyPeekCompleted(ByVal [source] As _
            [Object], ByVal asyncResult As PeekCompletedEventArgs)

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

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

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

            ' Restart the asynchronous peek operation.
            mq.BeginPeek()

            Return

        End Sub

End Class

注解

当你使用事件通知从队列异步查看 (不删除) 消息时, 必须创建处理消息处理的方法。When you use event notification to peek (read without removing) messages asynchronously from the queue, you must create a method that handles your message processing. 你的代码必须BeginPeek调用来开始异步处理。Your code must call BeginPeek to begin the asynchronous processing. 扫视消息时, 会通过PeekCompleted事件通知你的应用程序。When a message is peeked, your application is notified through the PeekCompleted event. PeekCompletedEventArgs实例被传递到调用事件处理程序的事件委托。An instance of PeekCompletedEventArgs is passed into the event delegate that calls your event handler. PeekCompleted事件关联的数据包含在委托的AsyncResult参数中。The data associated with the PeekCompleted event is contained in the delegate's AsyncResult parameter.

可以通过两种方式提供事件完成通知: 事件通知和回调。There are two ways to provide notification of event completion: event notification and callbacks. PeekCompletedEventArgs仅与事件通知一起使用。PeekCompletedEventArgs is used only with event notification. 有关回调和事件通知的比较信息, 请参阅 "事件与回调 "。For information comparing callbacks and event notification, see "Events vs. Callbacks" on MSDN.

PeekCompletedEventArgs提供对通过Message成员启动异步查看操作结束的消息的访问。PeekCompletedEventArgs provides access to the message that initiated the end of the asynchronous peek operation, through the Message member. 这是对消息的另一种访问权限, 其行为与对的调用MessageQueue.EndPeek大致相同。This is an alternate access to the message, and behaves much the same as a call to MessageQueue.EndPeek.

属性

AsyncResult

获取或设置请求的异步操作结果。Gets or sets the result of the asynchronous operation requested.

Message

获取同异步查看操作关联的消息。Gets the message associated with the asynchronous peek operation.

方法

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetHashCode()

用作默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ToString()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)

适用于

另请参阅