MessageQueue.EndPeek(IAsyncResult) Metod

Definition

Slutför den angivna asynkrona granskningsåtgärden.

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

Parametrar

asyncResult
IAsyncResult

Det IAsyncResult som identifierar den asynkrona granskningsåtgärden som ska slutföras och som ett slutresultat ska hämtas från.

Returer

Associerad Message med den slutförda asynkrona åtgärden.

Undantag

Parametern asyncResult är null.

Parameterns asyncResult syntax är ogiltig.

Ett fel uppstod vid åtkomst till en Message Queuing-metod.

Exempel

I följande kodexempel skapas en händelsehanterare med namnet MyPeekCompleted, kopplar den till händelsehanterardelegaten PeekCompleted och anropar BeginPeek för att initiera en asynkron granskningsåtgärd i kön som finns på sökvägen ".\myQueue". När en PeekCompleted händelse utlöses tittar exemplet på meddelandet och skriver dess brödtext till skärmen. Exemplet anropar BeginPeek sedan igen för att initiera en ny asynkron granskningsåtgärd.

#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

Kommentarer

När händelsen PeekCompleted aktiveras EndPeek(IAsyncResult) slutför den åtgärd som initierades av anropet BeginPeek . Det gör EndPeek(IAsyncResult) du genom att titta på meddelandet.

BeginPeek kan ange en timeout, vilket gör PeekCompleted att händelsen aktiveras om tidsgränsen inträffar innan ett meddelande visas i kön. När en timeout inträffar utan ett meddelande som kommer in i kön, utlöser ett efterföljande anrop till EndPeek(IAsyncResult) ett undantag.

EndPeek(IAsyncResult) används för att läsa meddelandet som orsakade PeekCompleted att händelsen aktiverades.

Om du vill fortsätta att asynkront granska meddelanden kan du anropa BeginPeek igen efter att du har anropat EndPeek(IAsyncResult).

I följande tabell visas om den här metoden är tillgänglig i olika arbetsgruppslägen.

Arbetsgruppsläge Tillgängligt
Lokal dator Yes
Namn på lokal dator och direktformat Yes
Fjärrdator No
Namn på fjärrdator och direktformat Yes

Gäller för

Se även