Export (0) Print
Expand All
0 out of 2 rated this helpful - Rate this topic

MessageQueue.PeekCompleted Event

Occurs when a message is read without being removed from the queue. This is a result of the asynchronous operation, BeginPeek.

Namespace: System.Messaging
Assembly: System.Messaging (in system.messaging.dll)

public:
event PeekCompletedEventHandler^ PeekCompleted {
	void add (PeekCompletedEventHandler^ value);
	void remove (PeekCompletedEventHandler^ value);
}
/** @event */
public void add_PeekCompleted (PeekCompletedEventHandler value)

/** @event */
public void remove_PeekCompleted (PeekCompletedEventHandler value)

JScript supports the use of events, but not the declaration of new ones.

BeginPeek is used in asynchronous processing to raise the PeekCompleted event when a message is available in the queue.

EndPeek is used to complete the operation initiated by a call to BeginPeek and peek the message when the PeekCompleted event is raised.

When you create a PeekCompletedEventHandler delegate, you identify the method that will handle the event. 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 Events and Delegates.

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". When a PeekCompleted event is raised, the example peeks the message and writes its body to the screen. 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;
}

package MyProject; 

import System.*;
import System.Messaging.*;

/// <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(String[] args)
    {
        // Create an instance of MessageQueue. Set its formatter.
        MessageQueue myQueue = new MessageQueue(".\\myQueue");
        myQueue.set_Formatter(new XmlMessageFormatter(new Type[]
            { String.class.ToType() }));
        // Add an event handler for the PeekCompleted event.
        myQueue.add_PeekCompleted(new PeekCompletedEventHandler(MyPeekCompleted));
        // Begin the asynchronous peek operation.
        myQueue.BeginPeek();
        // Do other work on the current thread.
        return;
    } //main

    //**************************************************
    // 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.get_AsyncResult());
        // Display message information on the screen.
        Console.WriteLine("Message: " + (String)(m.get_Body()));
        // Restart the asynchronous peek operation.
        mq.BeginPeek();
        return;
    } //MyPeekCompleted
} //MyNewQueue

  • Full trust for the immediate caller. This member cannot be used by partially trusted code. For more information, see .

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.