MessageQueue.EndPeek(IAsyncResult) Método

Definición

Completa la operación de búsqueda peek asincrónica especificada.

public:
 System::Messaging::Message ^ EndPeek(IAsyncResult ^ asyncResult);
public System.Messaging.Message EndPeek (IAsyncResult asyncResult);
member this.EndPeek : IAsyncResult -> System.Messaging.Message
Public Function EndPeek (asyncResult As IAsyncResult) As Message

Parámetros

asyncResult
IAsyncResult

El objeto IAsyncResult que identifica la operación de búsqueda peek asincrónica que va a terminar y de la que se va a recuperar un resultado final.

Devoluciones

Message asociado a la operación asincrónica terminada.

Excepciones

El parámetro asyncResult es null.

La sintaxis del parámetro asyncResult no es válida.

Error al obtener acceso a un método de Message Queuing.

Ejemplos

En el ejemplo de código siguiente se crea un controlador de eventos denominado MyPeekCompleted, se adjunta al delegado del PeekCompleted controlador de eventos y se llama BeginPeek a para iniciar una operación de inspección asincrónica en la cola que se encuentra en la ruta de acceso ".\myQueue". Cuando se genera un PeekCompleted evento, el ejemplo busca el mensaje y escribe su cuerpo en la pantalla. A continuación, se llama BeginPeek de nuevo al ejemplo para iniciar una nueva operación de inspección asincrónica.

#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

Comentarios

Cuando se genera el PeekCompleted evento, EndPeek(IAsyncResult) completa la operación iniciada por la BeginPeek llamada. Para ello, EndPeek(IAsyncResult) consulta el mensaje.

BeginPeek puede especificar un tiempo de espera, lo que hace que se genere el PeekCompleted evento si el tiempo de espera se produce antes de que aparezca un mensaje en la cola. Cuando se produce un tiempo de espera sin que llegue un mensaje a la cola, una llamada posterior a EndPeek(IAsyncResult) produce una excepción.

EndPeek(IAsyncResult) se usa para leer el mensaje que provocó el PeekCompleted evento.

Si desea seguir viendo los mensajes de forma asincrónica, puede volver a llamar BeginPeek a después de llamar a EndPeek(IAsyncResult).

En la tabla siguiente se muestra si este método está disponible en varios modos de grupo de trabajo.

Modo de grupo de trabajo Disponible
Equipo local
Equipo local y nombre de formato directo
Equipo remoto No
Equipo remoto y nombre de formato directo

Se aplica a

Consulte también