MessageQueue.PeekCompleted MessageQueue.PeekCompleted MessageQueue.PeekCompleted MessageQueue.PeekCompleted Event

定義

發生於訊息已讀取但未從佇列中移除時。Occurs when a message is read without being removed from the queue. 這是非同步作業 BeginPeek() 的結果。This is a result of the asynchronous operation, BeginPeek().

public:
 event System::Messaging::PeekCompletedEventHandler ^ PeekCompleted;
[System.Messaging.MessagingDescription("MQ_PeekCompleted")]
public event System.Messaging.PeekCompletedEventHandler PeekCompleted;
member this.PeekCompleted : System.Messaging.PeekCompletedEventHandler 
Public Custom Event PeekCompleted As PeekCompletedEventHandler 
屬性

範例

下列程式碼範例會建立名為MyPeekCompleted的事件處理常式、將它附加PeekCompleted至事件處理常式BeginPeek委派, 並呼叫, 以在位於路徑 ".\myQueue" 的佇列上起始非同步查看作業。The following code example creates an event handler named MyPeekCompleted, attaches it to the PeekCompleted event handler delegate, and calls BeginPeek to initiate an asynchronous peek operation on the queue that is located at the path ".\myQueue". 當引發PeekCompleted事件時, 此範例會查看訊息, 並將其主體寫入畫面。When a PeekCompleted event is raised, the example peeks the message and writes its body to the screen. 然後, 此範例BeginPeek會再次呼叫以起始新的非同步查看作業The example then calls BeginPeek again to initiate a new asynchronous peek operation

#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 'MyNewQueue

備註

BeginPeek當佇列中有可用的訊息時PeekCompleted , 會在非同步處理中用來引發事件。BeginPeek is used in asynchronous processing to raise the PeekCompleted event when a message is available in the queue.

EndPeek(IAsyncResult)是用來完成呼叫BeginPeek所起始的作業, 並PeekCompleted在引發事件時查看訊息。EndPeek(IAsyncResult) is used to complete the operation initiated by a call to BeginPeek and peek the message when the PeekCompleted event is raised.

建立 PeekCompletedEventHandler 委派時,必須識別處理事件的方法。When you create a PeekCompletedEventHandler delegate, you identify the method that will handle the event. 若要使事件與您的事件處理常式產生關聯,請將委派的執行個體 (Instance) 加入至事件。To associate the event with your event handler, add an instance of the delegate to the event. 除非您移除委派,否則每當事件發生時就會呼叫事件處理常式。The event handler is called whenever the event occurs, unless you remove the delegate. 如需事件處理常式委派的詳細資訊, 請參閱處理和引發事件For more information about event handler delegates, see Handling and Raising Events.

適用於

另請參閱