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

Message 与已完成的异步操作关联。The 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

适用于

另请参阅