MessageQueue.BeginPeek メソッド

定義

メッセージのピークを開始し、完了したときにイベント ハンドラーに通知するようにメッセージ キューに指示して、非同期のピーク操作を実行します。

オーバーロード

BeginPeek(TimeSpan, Object, AsyncCallback)

指定したタイムアウトと指定した状態オブジェクトを持つ非同期のピーク操作を実行します。状態オブジェクトは、操作の有効期間を通じて関連付けられた情報を提供します。 このオーバーロードは、コールバックを通じて操作のイベント ハンドラー ID の通知を受信します。 この操作は、メッセージをキューで使用できるようになるか、タイムアウトが発生するまで完了しません。

BeginPeek(TimeSpan, Object)

指定したタイムアウトと指定した状態オブジェクトを持つ非同期のピーク操作を実行します。状態オブジェクトは、操作の有効期間を通じて関連付けられた情報を提供します。 この操作は、メッセージをキューで使用できるようになるか、タイムアウトが発生するまで完了しません。

BeginPeek(TimeSpan, Cursor, PeekAction, Object, AsyncCallback)

指定したタイムアウトがあり、指定したカーソル、ピーク処理、および状態オブジェクトを使用する非同期のピーク操作を実行します。 状態オブジェクトは、操作の有効期間を通じて、関連付けられた情報を提供します。 このオーバーロードは、コールバックを通じて操作のイベント ハンドラー ID の通知を受信します。 この操作は、メッセージをキューで使用できるようになるか、タイムアウトが発生するまで完了しません。

BeginPeek()

タイムアウトのない非同期のピーク操作を実行します。この操作は、キューのメッセージが利用可能になるまで完了しません。

BeginPeek(TimeSpan)

指定したタイムアウトのある非同期のピーク操作を実行します。この操作は、メッセージをキューで使用できるようになるか、タイムアウトが発生するまで完了しません。

BeginPeek(TimeSpan, Object, AsyncCallback)

指定したタイムアウトと指定した状態オブジェクトを持つ非同期のピーク操作を実行します。状態オブジェクトは、操作の有効期間を通じて関連付けられた情報を提供します。 このオーバーロードは、コールバックを通じて操作のイベント ハンドラー ID の通知を受信します。 この操作は、メッセージをキューで使用できるようになるか、タイムアウトが発生するまで完了しません。

public:
 IAsyncResult ^ BeginPeek(TimeSpan timeout, System::Object ^ stateObject, AsyncCallback ^ callback);
public IAsyncResult BeginPeek (TimeSpan timeout, object stateObject, AsyncCallback callback);
member this.BeginPeek : TimeSpan * obj * AsyncCallback -> IAsyncResult
Public Function BeginPeek (timeout As TimeSpan, stateObject As Object, callback As AsyncCallback) As IAsyncResult

パラメーター

timeout
TimeSpan

メッセージを使用できるようになるまでの待機時間を示す TimeSpan

stateObject
Object

非同期操作に関連付けられている情報を保持する状態オブジェクト。アプリケーションで指定します。

callback
AsyncCallback

非同期操作の完了通知を受信する AsyncCallback

戻り値

ポストされた非同期要求を識別する IAsyncResult

例外

timeout パラメーターに指定された値は無効です。

メッセージ キューのメソッドにアクセスしたときにエラーが発生しました。

次のコード例では、非同期のピーク操作を作成します。 このコード例では、ローカル メッセージ キューにメッセージを送信し、 を呼び出BeginPeek(TimeSpan, Object, AsyncCallback)します。タイムアウト値は 10 秒、特定のメッセージを識別する一意の整数、イベント ハンドラーMyPeekCompletedを識別する のAsyncCallback新しいインスタンスを渡します。 イベントが PeekCompleted 発生すると、イベント ハンドラーはメッセージをピークし、メッセージ本文と整数メッセージ識別子を画面に書き込みます。

#using <System.Messaging.dll>
#using <System.dll>

using namespace System;
using namespace System::Messaging;

// Creates a new queue.
void CreateQueue(String^ queuePath, bool transactional)
{
    if(!MessageQueue::Exists(queuePath))
    {
        MessageQueue^ queue = MessageQueue::Create(queuePath, transactional);
        queue->Close();      
    }
    else
    {
        Console::WriteLine("{0} already exists.", queuePath);
    }
}

// Provides an event handler for the PeekCompleted event.
void MyPeekCompleted(IAsyncResult^ asyncResult)
{
    // Connect to the queue.
    MessageQueue^ queue = gcnew MessageQueue(".\\exampleQueue");

    // End the asynchronous peek operation.
    Message^ msg = queue->EndPeek(asyncResult);

    // Display the message information on the screen.
    Console::WriteLine("Message number: {0}", asyncResult->AsyncState);
    Console::WriteLine("Message body: {0}", msg->Body);

    // Receive the message. This will remove the message from the queue.
    msg = queue->Receive(TimeSpan::FromSeconds(10.0));

    queue->Close();
}

int main()
{
    // Represents a state object associated with each message.
    int messageNumber = 0;

    // Create a non-transactional queue on the local computer.
    // Note that the queue might not be immediately accessible, and
    // therefore this example might throw an exception of type
    // System.Messaging.MessageQueueException when trying to send a
    // message to the newly created queue.
    CreateQueue(".\\exampleQueue", false);

    // Connect to a queue on the local computer.
    MessageQueue^ queue = gcnew MessageQueue(".\\exampleQueue");

    // Send a message to the queue.
    queue->Send("Example Message");

    // Begin the asynchronous peek operation.
    queue->BeginPeek(TimeSpan::FromSeconds(10.0), messageNumber++,
        gcnew AsyncCallback(MyPeekCompleted));

    // Simulate doing other work on the current thread.
    System::Threading::Thread::Sleep(TimeSpan::FromSeconds(10.0));

    queue->Close();
}
using System;
using System.Messaging;

public class QueueExample
{
    // Represents a state object associated with each message.
    static int messageNumber = 0;

    public static void Main()
    {
        // Create a non-transactional queue on the local computer.
        // Note that the queue might not be immediately accessible, and
        // therefore this example might throw an exception of type
        // System.Messaging.MessageQueueException when trying to send a
        // message to the newly created queue.
        CreateQueue(".\\exampleQueue", false);

        // Connect to a queue on the local computer.
        MessageQueue queue = new MessageQueue(".\\exampleQueue");

        // Send a message to the queue.
        queue.Send("Example Message");

        // Begin the asynchronous peek operation.
        queue.BeginPeek(TimeSpan.FromSeconds(10.0), messageNumber++,
            new AsyncCallback(MyPeekCompleted));

        // Simulate doing other work on the current thread.
        System.Threading.Thread.Sleep(TimeSpan.FromSeconds(10.0));

        return;
    }

    // Creates a new queue.
    public static void CreateQueue(string queuePath, bool transactional)
    {
        if(!MessageQueue.Exists(queuePath))
        {
            MessageQueue.Create(queuePath, transactional);
        }
        else
        {
            Console.WriteLine(queuePath + " already exists.");
        }
    }

    // Provides an event handler for the PeekCompleted event.
    private static void MyPeekCompleted(IAsyncResult asyncResult)
    {
        // Connect to the queue.
        MessageQueue queue = new MessageQueue(".\\exampleQueue");

        // End the asynchronous peek operation.
        Message msg = queue.EndPeek(asyncResult);

        // Display the message information on the screen.
        Console.WriteLine("Message number: {0}", (int)asyncResult.AsyncState);
        Console.WriteLine("Message body: {0}", (string)msg.Body);

        // Receive the message. This will remove the message from the queue.
        msg = queue.Receive(TimeSpan.FromSeconds(10.0));
    }
}

注釈

このオーバーロードを使用すると、コールバック パラメーターで指定されたコールバックは、メッセージがキューで使用可能になったとき、または指定した時間間隔が経過したときに直接呼び出されます。 PeekCompleted イベントは発生しません。 の他の BeginPeek オーバーロードは、イベントを発生させるためにこのコンポーネントに PeekCompleted 依存します。

PeekCompleted は、メッセージがキューに既に存在する場合にも発生します。

メソッドは BeginPeek 直ちにを返しますが、イベント ハンドラーが呼び出されるまで非同期操作は完了しません。

は非同期であるため BeginPeek 、現在の実行スレッドをブロックすることなく、それを呼び出してキューをピークできます。 キューを同期的にピークするには、 メソッドを使用します Peek

非同期操作が完了したら、イベント ハンドラーで または BeginReceive をもう一度呼び出BeginPeekして、通知を受信し続けることができます。

BeginPeek は、 IAsyncResult メソッドが開始した非同期操作を識別する を返します。 これは IAsyncResult 操作の有効期間中に使用できますが、通常は が呼び出されるまで EndPeek(IAsyncResult) 使用しません。 ただし、複数の非同期操作を開始する場合は、その値を IAsyncResult 配列に配置し、すべての操作または任意の操作が完了するまで待機するかどうかを指定できます。 この場合は、 の IAsyncResult プロパティをAsyncWaitHandle使用して、完了した操作を識別します。

state オブジェクトは、状態情報を操作に関連付けます。 たとえば、複数回を呼び出 BeginPeek して複数の操作を開始する場合は、定義した個別の状態オブジェクトを使用して各操作を識別できます。

次の表は、このメソッドがさまざまなワークグループ モードで使用できるかどうかを示しています。

ワークグループ モード 利用可能
ローカル コンピューター はい
ローカル コンピューターと直接の形式名 はい
リモート コンピューター いいえ
リモート コンピューターと直接形式の名前 はい

こちらもご覧ください

適用対象

BeginPeek(TimeSpan, Object)

指定したタイムアウトと指定した状態オブジェクトを持つ非同期のピーク操作を実行します。状態オブジェクトは、操作の有効期間を通じて関連付けられた情報を提供します。 この操作は、メッセージをキューで使用できるようになるか、タイムアウトが発生するまで完了しません。

public:
 IAsyncResult ^ BeginPeek(TimeSpan timeout, System::Object ^ stateObject);
public IAsyncResult BeginPeek (TimeSpan timeout, object stateObject);
member this.BeginPeek : TimeSpan * obj -> IAsyncResult
Public Function BeginPeek (timeout As TimeSpan, stateObject As Object) As IAsyncResult

パラメーター

timeout
TimeSpan

メッセージを使用できるようになるまでの待機時間を示す TimeSpan

stateObject
Object

非同期操作に関連付けられている情報を保持する状態オブジェクト。アプリケーションで指定します。

戻り値

ポストされた非同期要求を識別する IAsyncResult

例外

timeout パラメーターに指定された値は無効です。

メッセージ キューのメソッドにアクセスしたときにエラーが発生しました。

次のコード例では、キュー パス ".\myQueue" を使用して非同期ピーク操作を作成します。 イベント ハンドラー を作成し、 MyPeekCompletedイベント ハンドラー デリゲートに PeekCompleted アタッチします。 BeginPeek が呼び出され、タイムアウトは 1 分です。 の BeginPeek 各呼び出しには、その特定の操作を識別する一意の関連付けられた整数があります。 イベントが PeekCompleted 発生したとき、またはタイムアウトが切れると、メッセージが存在する場合は、メッセージが取得され、その本文と操作固有の整数識別子が画面に書き込まれます。 次 BeginPeek に、もう一度呼び出して、完了した操作の同じタイムアウトと関連付けられた整数を使用して、新しい非同期ピーク操作を開始します。

#using <system.dll>
#using <system.messaging.dll>

using namespace System;
using namespace System::Messaging;
ref class MyNewQueue
{
public:

   // Represents a state object associated with each message.
   static int messageNumber = 0;

   // Provides an event handler for the PeekCompleted
   // event.
   //
   static void MyPeekCompleted( Object^ source, PeekCompletedEventArgs^ asyncResult )
   {
      try
      {
         // 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, 
         // including the message number (state object).
         Console::WriteLine( "Message: {0} {1}", asyncResult->AsyncResult->AsyncState, static_cast<String^>(m->Body) );

         // Restart the asynchronous peek operation, with the 
         // same time-out.
         mq->BeginPeek( TimeSpan(0,1,0), messageNumber++ );
      }
      catch ( MessageQueueException^ e ) 
      {
         if ( e->MessageQueueErrorCode == MessageQueueErrorCode::IOTimeout )
         {
            Console::WriteLine( e );
         }

         // Handle other sources of MessageQueueException.
      }

      // Handle other exceptions.
      return;
   }
};


// Provides an entry point into the application.
//         
// This example performs asynchronous peek operation
// processing.
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 with a timeout 
   // of one minute.
   myQueue->BeginPeek( TimeSpan(0,1,0), MyNewQueue::messageNumber++ );

   // 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 MyNewQueue3
    {
        // Represents a state object associated with each message.
        static int messageNumber = 0;

        //**************************************************
        // 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 with a time-out
            // of one minute.
            myQueue.BeginPeek(new TimeSpan(0, 1, 0), messageNumber++);

            // Do other work on the current thread.

            return;
        }

        //**************************************************
        // Provides an event handler for the PeekCompleted
        // event.
        //**************************************************

        private static void MyPeekCompleted(Object source,
            PeekCompletedEventArgs asyncResult)
        {
            try
            {
                // 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,
                // including the message number (state object).
                Console.WriteLine("Message: " +
                    (int)asyncResult.AsyncResult.AsyncState + " "
                    + (string)m.Body);

                // Restart the asynchronous peek operation, with the
                // same time-out.
                mq.BeginPeek(new TimeSpan(0, 1, 0), messageNumber++);
            }

            catch (MessageQueueException e)
            {
                if (e.MessageQueueErrorCode ==
                    MessageQueueErrorCode.IOTimeout)
                {
                    Console.WriteLine(e.ToString());
                }

                // Handle other sources of MessageQueueException.
            }

            // Handle other exceptions.

            return;
        }
    }
}
Imports System.Messaging


   
' Provides a container class for the example.

Public Class MyNewQueue

        ' Represents a state object associated with each message.
        Private Shared messageNumber As Integer = 0



        ' 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 with a time-out 
            ' of one minute.
            myQueue.BeginPeek(New TimeSpan(0, 1, 0), messageNumber)
            messageNumber += 1

            ' 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)

            Try
                ' 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, 
                ' including(the) message number (state object).
                Console.WriteLine(("Message: " + _
                    CInt(asyncResult.AsyncResult.AsyncState) + _
                    " " + CStr(m.Body)))

                ' Restart the asynchronous peek operation, with the 
                ' same time-out.
                mq.BeginPeek(New TimeSpan(0, 1, 0), messageNumber)
                messageNumber += 1


            Catch e As MessageQueueException

                If e.MessageQueueErrorCode = _
                    MessageQueueErrorCode.IOTimeout Then

                    Console.WriteLine(e.ToString())

                    ' Handle other sources of MessageQueueException.

                End If

                ' Handle other exceptions.

            End Try

            Return

        End Sub

End Class

注釈

非同期処理では、 を使用 BeginPeek して、キューでメッセージが使用可能になったとき、または指定した時間間隔の有効期限が切れたときに イベントを発生 PeekCompleted させます。

PeekCompleted は、メッセージがキューに既に存在する場合にも発生します。

このオーバーロードを使用して、操作の有効期間中に保持される操作に情報を関連付けます。 イベント ハンドラーは、操作に関連付けられている の IAsyncResult プロパティをAsyncState調べることで、この情報にアクセスできます。

を使用 BeginPeekするには、非同期操作の結果を処理するイベント ハンドラーを作成し、それをイベント デリゲートに関連付けます。 BeginPeek は非同期ピーク操作を開始します。 MessageQueue は、メッセージがキューに到着したときに、イベントの PeekCompleted 発生を通じて通知されます。 MessageQueueその後、 を呼び出EndPeek(IAsyncResult)すか、 を使用して結果を取得することで、メッセージにPeekCompletedEventArgsアクセスできます。

メソッドは BeginPeek 直ちにを返しますが、イベント ハンドラーが呼び出されるまで非同期操作は完了しません。

は非同期であるため BeginPeek 、現在の実行スレッドをブロックすることなく、それを呼び出してキューをピークできます。 キューを同期的にピークするには、 メソッドを使用します Peek

非同期操作が完了したら、イベント ハンドラーで または BeginReceive をもう一度呼び出BeginPeekして、通知を受信し続けることができます。

BeginPeek は、 IAsyncResult メソッドが開始した非同期操作を識別する を返します。 これは IAsyncResult 操作の有効期間中に使用できますが、通常は が呼び出されるまで EndPeek(IAsyncResult) 使用しません。 ただし、複数の非同期操作を開始する場合は、その値を IAsyncResult 配列に配置し、すべての操作または任意の操作が完了するまで待機するかどうかを指定できます。 この場合は、 の IAsyncResult プロパティをAsyncWaitHandle使用して、完了した操作を識別します。

このオーバーロードは、タイムアウトと状態オブジェクトを指定します。 パラメーターで指定された間隔が timeout 期限切れになると、このコンポーネントは イベントを PeekCompleted 発生させます。 メッセージが存在しないため、 の後続の呼び出し EndPeek(IAsyncResult) では例外がスローされます。

state オブジェクトは、状態情報を操作に関連付けます。 たとえば、複数回を呼び出 BeginPeek して複数の操作を開始する場合は、定義した個別の状態オブジェクトを使用して各操作を識別できます。 このシナリオの図については、「例」セクションを参照してください。

state オブジェクトを使用して、プロセス スレッド間で情報を渡すこともできます。 スレッドが開始されていても、コールバックが非同期シナリオで別のスレッドにある場合、状態オブジェクトはマーシャリングされ、イベントからの情報と共に返されます。

falseの場合CanRead、完了イベントが発生しますが、 を呼び出EndPeek(IAsyncResult)すと例外がスローされます。

次の表は、このメソッドがさまざまなワークグループ モードで使用できるかどうかを示しています。

ワークグループ モード 利用可能
ローカル コンピューター はい
ローカル コンピューターと直接の形式名 はい
リモート コンピューター いいえ
リモート コンピューターと直接形式の名前 はい

こちらもご覧ください

適用対象

BeginPeek(TimeSpan, Cursor, PeekAction, Object, AsyncCallback)

指定したタイムアウトがあり、指定したカーソル、ピーク処理、および状態オブジェクトを使用する非同期のピーク操作を実行します。 状態オブジェクトは、操作の有効期間を通じて、関連付けられた情報を提供します。 このオーバーロードは、コールバックを通じて操作のイベント ハンドラー ID の通知を受信します。 この操作は、メッセージをキューで使用できるようになるか、タイムアウトが発生するまで完了しません。

public:
 IAsyncResult ^ BeginPeek(TimeSpan timeout, System::Messaging::Cursor ^ cursor, System::Messaging::PeekAction action, System::Object ^ state, AsyncCallback ^ callback);
public IAsyncResult BeginPeek (TimeSpan timeout, System.Messaging.Cursor cursor, System.Messaging.PeekAction action, object state, AsyncCallback callback);
member this.BeginPeek : TimeSpan * System.Messaging.Cursor * System.Messaging.PeekAction * obj * AsyncCallback -> IAsyncResult
Public Function BeginPeek (timeout As TimeSpan, cursor As Cursor, action As PeekAction, state As Object, callback As AsyncCallback) As IAsyncResult

パラメーター

timeout
TimeSpan

メッセージを使用できるようになるまでの待機時間を示す TimeSpan

cursor
Cursor

メッセージ キュー内の特定の位置を維持する Cursor

action
PeekAction

PeekAction 値のいずれか 1 つ。 キュー内の現在のメッセージと次のメッセージのどちらをピークするかを示します。

state
Object

非同期操作に関連付けられている情報を保持する状態オブジェクト。アプリケーションで指定します。

callback
AsyncCallback

非同期操作の完了通知を受信する AsyncCallback

戻り値

ポストされた非同期要求を識別する IAsyncResult

例外

action パラメーターに PeekAction.Current または PeekAction.Next 以外の値が指定されました。

cursor パラメーターが null です。

timeout パラメーターに指定された値は無効です。

メッセージ キューのメソッドにアクセスしたときにエラーが発生しました。

注釈

このオーバーロードを使用すると、コールバック パラメーターで指定されたコールバックは、メッセージがキューで使用可能になったとき、または指定した時間間隔の有効期限が切れたときに直接呼び出されます。 イベントは PeekCompleted 発生しません。 の他の BeginPeek オーバーロードは、イベントを発生させるためにこのコンポーネントに PeekCompleted 依存します。

PeekCompleted は、メッセージがキューに既に存在する場合にも発生します。

メソッドは BeginPeek 直ちにを返しますが、イベント ハンドラーが呼び出されるまで非同期操作は完了しません。

は非同期であるため BeginPeek 、現在の実行スレッドをブロックすることなく、それを呼び出してキューをピークできます。 キューを同期的にピークするには、 メソッドを使用します Peek

非同期操作が完了したら、イベント ハンドラーで または BeginReceive をもう一度呼び出BeginPeekして、通知を受信し続けることができます。

BeginPeek は、 IAsyncResult メソッドによって開始された非同期操作を識別する を返します。 これは IAsyncResult 操作の有効期間中に使用できますが、通常は が呼び出されるまで EndPeek(IAsyncResult) 使用しません。 ただし、複数の非同期操作を開始する場合は、その値を IAsyncResult 配列に配置し、すべての操作または任意の操作が完了するまで待機するかどうかを指定できます。 この場合は、 の IAsyncResult プロパティをAsyncWaitHandle使用して、完了した操作を識別します。

state オブジェクトは、状態情報を操作に関連付けます。 たとえば、複数回を呼び出 BeginPeek して複数の操作を開始する場合は、定義した個別の状態オブジェクトを使用して各操作を識別できます。

次の表は、このメソッドがさまざまなワークグループ モードで使用できるかどうかを示しています。

ワークグループ モード 利用可能
ローカル コンピューター はい
ローカル コンピューターと直接の形式名 はい
リモート コンピューター いいえ
リモート コンピューターと直接形式の名前 はい

こちらもご覧ください

適用対象

BeginPeek()

タイムアウトのない非同期のピーク操作を実行します。この操作は、キューのメッセージが利用可能になるまで完了しません。

public:
 IAsyncResult ^ BeginPeek();
public IAsyncResult BeginPeek ();
member this.BeginPeek : unit -> IAsyncResult
Public Function BeginPeek () As IAsyncResult

戻り値

ポストされた非同期要求を識別する IAsyncResult

例外

メッセージ キューのメソッドにアクセスしたときにエラーが発生しました。

次のコード例では、 という名前 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

注釈

非同期処理では、 を使用 BeginPeek して、キューで PeekCompleted メッセージが使用可能になったときに イベントを発生させます。

PeekCompleted は、メッセージがキューに既に存在する場合にも発生します。

を使用 BeginPeekするには、非同期操作の結果を処理するイベント ハンドラーを作成し、それをイベント デリゲートに関連付けます。 BeginPeek は非同期ピーク操作を開始します。 MessageQueue は、メッセージがキューに到着したときに、イベントの PeekCompleted 発生を通じて通知されます。 MessageQueueその後、 を呼び出EndPeek(IAsyncResult)すか、 を使用して結果を取得することで、メッセージにPeekCompletedEventArgsアクセスできます。

メソッドは BeginPeek 直ちにを返しますが、イベント ハンドラーが呼び出されるまで非同期操作は完了しません。

は非同期であるため BeginPeek 、現在の実行スレッドをブロックすることなく、それを呼び出してキューをピークできます。 キューを同期的にピークするには、 メソッドを使用します Peek

非同期操作が完了したら、イベント ハンドラーで または BeginReceive をもう一度呼び出BeginPeekして、通知を受信し続けることができます。

IAsyncResultを返す BeginPeek は、メソッドが開始した非同期操作を識別します。 これは IAsyncResult 操作の有効期間中に使用できますが、通常は が呼び出されるまで EndPeek(IAsyncResult) 使用しません。 ただし、複数の非同期操作を開始する場合は、その値を IAsyncResult 配列に配置し、すべての操作または任意の操作が完了するまで待機するかどうかを指定できます。 この場合は、 の IAsyncResult プロパティをAsyncWaitHandle使用して、完了した操作を識別します。

falseの場合CanRead、完了イベントが発生しますが、 を呼び出EndPeek(IAsyncResult)すと例外がスローされます。

次の表は、このメソッドがさまざまなワークグループ モードで使用できるかどうかを示しています。

ワークグループ モード 利用可能
ローカル コンピューター はい
ローカル コンピューターと直接の形式名 はい
リモート コンピューター いいえ
リモート コンピューターと直接形式の名前 はい

こちらもご覧ください

適用対象

BeginPeek(TimeSpan)

指定したタイムアウトのある非同期のピーク操作を実行します。この操作は、メッセージをキューで使用できるようになるか、タイムアウトが発生するまで完了しません。

public:
 IAsyncResult ^ BeginPeek(TimeSpan timeout);
public IAsyncResult BeginPeek (TimeSpan timeout);
member this.BeginPeek : TimeSpan -> IAsyncResult
Public Function BeginPeek (timeout As TimeSpan) As IAsyncResult

パラメーター

timeout
TimeSpan

メッセージを使用できるようになるまでの待機時間を示す TimeSpan

戻り値

ポストされた非同期要求を識別する IAsyncResult

例外

timeout パラメーターに指定された値は無効です。

メッセージ キューのメソッドにアクセスしたときにエラーが発生しました。

次のコード例では、キュー パス ".\myQueue" を使用して非同期ピーク操作を作成します。 イベント ハンドラー を作成し、 MyPeekCompletedイベント ハンドラー デリゲートに PeekCompleted アタッチします。 BeginPeek は、非同期ピーク操作を開始するために、1 分のタイムアウトで呼び出されます。 イベントが PeekCompleted 発生するか、タイムアウトが切れると、メッセージが存在する場合にメッセージが取得され、その本文が画面に書き込まれます。 次 BeginPeek に、 が再度呼び出され、同じタイムアウトで新しい非同期ピーク操作が開始されます。

#using <system.dll>
#using <system.messaging.dll>

using namespace System;
using namespace System::Messaging;
ref class MyNewQueue
{
public:
   static void MyPeekCompleted( Object^ source, PeekCompletedEventArgs^ asyncResult )
   {      try
      {
         // 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, with the 
         // same time-out.
         mq->BeginPeek( TimeSpan(0,1,0) );
      }
      catch ( MessageQueueException^ e ) 
      {
         if ( e->MessageQueueErrorCode == MessageQueueErrorCode::IOTimeout )
         {
            Console::WriteLine( e );
         }

         // Handle other sources of MessageQueueException.
      }

      // Handle other exceptions.
      return;
   }
};

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 with a timeout 
   // of one minute.
   myQueue->BeginPeek( TimeSpan(0,1,0) );

   // 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 MyNewQueue2
    {
        //**************************************************
        // 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 with a time-out
            // of one minute.
            myQueue.BeginPeek(new TimeSpan(0, 1, 0));

            // Do other work on the current thread.

            return;
        }

        //**************************************************
        // Provides an event handler for the PeekCompleted
        // event.
        //**************************************************

        private static void MyPeekCompleted(Object source,
            PeekCompletedEventArgs asyncResult)
        {
            try
            {
                // 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, with the
                // same time-out.
                mq.BeginPeek(new TimeSpan(0, 1, 0));
            }

            catch (MessageQueueException e)
            {
                if (e.MessageQueueErrorCode ==
                    MessageQueueErrorCode.IOTimeout)
                {
                    Console.WriteLine(e.ToString());
                }

                // Handle other sources of MessageQueueException.
            }

            // Handle other exceptions.

            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 with a time-out 
            ' of one minute.
            myQueue.BeginPeek(New TimeSpan(0, 1, 0))

            ' 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)

            Try
                ' 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, with the 
                ' same time-out.
                mq.BeginPeek(New TimeSpan(0, 1, 0))

            Catch e As MessageQueueException

                If e.MessageQueueErrorCode = _
                    MessageQueueErrorCode.IOTimeout Then

                    Console.WriteLine(e.ToString())

                    ' Handle other sources of MessageQueueException.
                End If

                ' Handle other exceptions.

            End Try

            Return

        End Sub

End Class

注釈

非同期処理では、 を使用 BeginPeek して、キューでメッセージが使用可能になったとき、または指定した時間間隔の有効期限が切れたときに イベントを発生 PeekCompleted させます。

PeekCompleted は、メッセージがキューに既に存在する場合にも発生します。

を使用 BeginPeekするには、非同期操作の結果を処理するイベント ハンドラーを作成し、それをイベント デリゲートに関連付けます。 BeginPeek は非同期ピーク操作を開始します。 MessageQueue は、メッセージがキューに到着したときに、イベントの PeekCompleted 発生を通じて通知されます。 MessageQueueその後、 を呼び出EndPeek(IAsyncResult)すか、 を使用して結果を取得することで、メッセージにPeekCompletedEventArgsアクセスできます。

メソッドは BeginPeek 直ちにを返しますが、イベント ハンドラーが呼び出されるまで非同期操作は完了しません。

は非同期であるため BeginPeek 、現在の実行スレッドをブロックすることなく、それを呼び出してキューをピークできます。 キューを同期的にピークするには、 メソッドを使用します Peek

非同期操作が完了したら、イベント ハンドラーで または BeginReceive をもう一度呼び出BeginPeekして、通知を受信し続けることができます。

IAsyncResultを返す BeginPeek は、メソッドが開始した非同期操作を識別します。 これは IAsyncResult 操作の有効期間中に使用できますが、通常は が呼び出されるまで EndPeek(IAsyncResult) 使用しません。 ただし、複数の非同期操作を開始する場合は、その値を IAsyncResult 配列に配置し、すべての操作または任意の操作が完了するまで待機するかどうかを指定できます。 この場合は、 の IAsyncResult プロパティをAsyncWaitHandle使用して、完了した操作を識別します。

このオーバーロードは、タイムアウトを指定します。パラメーターで指定された間隔が timeout 期限切れになると、このコンポーネントは イベントを PeekCompleted 発生させます。 メッセージが存在しないため、 の後続の呼び出し EndPeek(IAsyncResult) では例外がスローされます。

falseの場合CanRead、完了イベントが発生しますが、 を呼び出EndPeek(IAsyncResult)すと例外がスローされます。

次の表は、このメソッドがさまざまなワークグループ モードで使用できるかどうかを示しています。

ワークグループ モード 利用可能
ローカル コンピューター はい
ローカル コンピューターと直接の形式名 はい
リモート コンピューター いいえ
リモート コンピューターと直接形式の名前 はい

こちらもご覧ください

適用対象

スレッド セーフ

メソッドはスレッド セーフではありません。