PeekCompletedEventArgs 類別

定義

提供 PeekCompleted 事件的資料。Provides data for the PeekCompleted event. 當非同步窺視作業 (Asynchronous Peek Operation) 呼叫事件處理常式時,會將這個類別的執行個體 (Instance) 傳遞至處理常式。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. 如需比較回呼和事件通知的相關資訊, 請參閱事件與回呼「在 MSDN 上」。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 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)

適用於

另請參閱