MessageQueue.EndPeek(IAsyncResult) Metodo

Definizione

Completa l'operazione di visualizzazione asincrona specificata.

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

Parametri

asyncResult
IAsyncResult

IAsyncResult che identifica l'operazione di visualizzazione asincrona da completare e da cui recuperare un risultato finale.

Restituisce

Message associato all'operazione asincrona completata.

Eccezioni

Il valore del parametro asyncResult è null.

La sintassi del parametro asyncResult non è valida.

Si è verificato un errore durante l'accesso a un metodo di Accodamento messaggi.

Esempio

L'esempio di codice seguente crea un gestore eventi denominato MyPeekCompleted, lo associa al delegato del PeekCompleted gestore eventi e chiama BeginPeek per avviare un'operazione di visualizzazione asincrona nella coda che si trova nel percorso ".\myQueue". Quando viene generato un PeekCompleted evento, l'esempio visualizza il messaggio e ne scrive il corpo sullo schermo. L'esempio chiama BeginPeek di nuovo per avviare una nuova operazione di visualizzazione asincrona.

#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

Commenti

Quando viene generato l'evento PeekCompleted , EndPeek(IAsyncResult) completa l'operazione avviata dalla BeginPeek chiamata. A tale scopo, EndPeek(IAsyncResult) visualizza il messaggio.

BeginPeek può specificare un timeout, che fa sì che l'evento PeekCompleted venga generato se si verifica il timeout prima che venga visualizzato un messaggio nella coda. Quando si verifica un timeout senza un messaggio in arrivo nella coda, una chiamata successiva a EndPeek(IAsyncResult) genera un'eccezione.

EndPeek(IAsyncResult) viene utilizzato per leggere il messaggio che ha causato la generazione dell'evento PeekCompleted .

Se si desidera continuare a visualizzare i messaggi in modo asincrono, è possibile chiamare di nuovo dopo aver chiamato BeginPeekEndPeek(IAsyncResult).

Nella tabella seguente viene illustrato se questo metodo è disponibile in varie modalità gruppo di lavoro.

Modalità gruppo di lavoro Disponibile
Computer locale
Computer locale e nome del formato diretto
Computer remoto No
Nome del formato diretto e del computer remoto

Si applica a

Vedi anche