MessageQueue.EndPeek(IAsyncResult) MessageQueue.EndPeek(IAsyncResult) MessageQueue.EndPeek(IAsyncResult) MessageQueue.EndPeek(IAsyncResult) Method

定義

完成指定的非同步窺視作業。Completes the specified asynchronous peek operation.

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

參數

asyncResult
IAsyncResult IAsyncResult IAsyncResult IAsyncResult

IAsyncResult,識別要完成的非同步窺視作業,並要從其中擷取最終結果。The IAsyncResult that identifies the asynchronous peek operation to finish and from which to retrieve an end result.

傳回

與完成的非同步作業相關的 MessageThe Message associated with the completed asynchronous operation.

例外狀況

asyncResult 參數為 nullThe asyncResult parameter is null.

asyncResult 參數的語法無效。The syntax of the asyncResult parameter is not valid.

存取訊息佇列方法時發生錯誤。An error occurred when accessing a Message Queuing method.

範例

下列程式碼範例會建立名為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

備註

當引發EndPeek(IAsyncResult)事件時, 會完成BeginPeek呼叫所起始的作業。 PeekCompletedWhen the PeekCompleted event is raised, EndPeek(IAsyncResult) completes the operation that was initiated by the BeginPeek call. 若要這麼做EndPeek(IAsyncResult) , 請查看訊息。To do so, EndPeek(IAsyncResult) peeks the message.

BeginPeek可以指定超時時間, 如果在佇列中出現PeekCompleted訊息之前發生超時, 則會引發事件。BeginPeek can specify a time-out, which causes the PeekCompleted event to be raised if the time-out occurs before a message appears in the queue. 當發生超時而沒有訊息抵達佇列時, 後續的呼叫EndPeek(IAsyncResult)會擲回例外狀況。When a time-out occurs without a message arriving in the queue, a subsequent call to EndPeek(IAsyncResult) throws an exception.

EndPeek(IAsyncResult)是用來讀取導致PeekCompleted引發事件的訊息。EndPeek(IAsyncResult) is used to read the message that caused the PeekCompleted event to be raised.

如果您想要繼續以非同步方式查看訊息, 您可以在BeginPeek呼叫EndPeek(IAsyncResult)之後再次呼叫。If you want to continue to asynchronously peek messages, you can again call BeginPeek after calling EndPeek(IAsyncResult).

下表顯示這個方法是否可在各種工作組模式中使用。The following table shows whether this method is available in various Workgroup modes.

工作組模式Workgroup mode 可用Available
本機電腦Local computer Yes
本機電腦和直接格式名稱Local computer and direct format name Yes
遠端電腦Remote computer No
遠端電腦和直接格式名稱Remote computer and direct format name Yes

適用於

另請參閱