MessageQueue.EndPeek(IAsyncResult) 方法

定义

完成指定的异步查看操作。

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 标识要完成的异步查看操作,并且从它检索最终结果。

返回

Message 与已完成的异步操作关联。

例外

asyncResult 参数为 null

asyncResult 参数的语法无效。

访问“消息队列”方法时出错。

示例

下面的代码示例创建名为 的 MyPeekCompleted事件处理程序,将其附加到 PeekCompleted 事件处理程序委托,并调用 BeginPeek 以对位于路径“.\myQueue”的队列启动异步速览操作。 当引发事件 PeekCompleted 时,该示例将速览消息并将其正文写入屏幕。 然后,该示例再次调用 BeginPeek 以启动新的异步速览操作。

#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)完成调用BeginPeek启动的操作。 为此, EndPeek(IAsyncResult) 请速览消息。

BeginPeek 可以指定超时,如果超时发生在消息出现在队列中之前,则会导致 PeekCompleted 引发事件。 如果发生超时,但消息到达队列时,对 的后续调用 EndPeek(IAsyncResult) 将引发异常。

EndPeek(IAsyncResult) 用于读取导致引发事件 PeekCompleted 的消息。

如果要继续异步查看消息,可以在调用 后再次调用 BeginPeekEndPeek(IAsyncResult)

下表显示了此方法在各种工作组模式下是否可用。

工作组模式 可用
本地计算机
本地计算机和直接格式名称
远程计算机
远程计算机和直接格式名称

适用于

另请参阅