MessageQueue.EndPeek(IAsyncResult) 方法

定義

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

傳回

Message

與完成的非同步作業相關的 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

備註

PeekCompleted引發事件時, EndPeek(IAsyncResult) 完成呼叫所起始的作業 BeginPeekWhen 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

適用於

另請參閱