MessageQueue.Peek メソッド

定義

キューにある最初のメッセージのコピーを返します。メッセージはキューから削除されません。

オーバーロード

Peek()

この MessageQueue が参照するキューにある最初のメッセージを、削除せずに返します (ピークします)。 Peek() メソッドは同期メソッドであるため、メッセージが利用可能になるまで、現在のスレッドをブロックします。

Peek(TimeSpan)

この MessageQueue が参照するキューにある最初のメッセージを、削除せずに返します (ピークします)。 Peek() メソッドは同期メソッドであるため、メッセージが利用可能になるか、指定したタイムアウトが発生するまで、現在のスレッドをブロックします。

Peek(TimeSpan, Cursor, PeekAction)

指定されたカーソルを使用して、キュー内の現在のメッセージまたは次のメッセージを、削除せずに返します (ピークします)。 Peek() メソッドは同期メソッドであるため、メッセージが利用可能になるか、指定したタイムアウトが発生するまで、現在のスレッドをブロックします。

Peek()

この MessageQueue が参照するキューにある最初のメッセージを、削除せずに返します (ピークします)。 Peek() メソッドは同期メソッドであるため、メッセージが利用可能になるまで、現在のスレッドをブロックします。

public:
 System::Messaging::Message ^ Peek();
public System.Messaging.Message Peek ();
member this.Peek : unit -> System.Messaging.Message
Public Function Peek () As Message

戻り値

キューの最初のメッセージを表す Message

例外

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

次の例では、キューで Peek メソッドを使用します。

最初の例では、アプリケーションはメッセージがキューで使用可能になるまで待機します。 最初の例では、到着したメッセージにアクセスしないことに注意してください。メッセージが到着するまで処理を一時停止するだけです。 メッセージがキューに既に存在する場合は、すぐに返されます。

2 番目の例では、アプリケーション定義 Order クラスを含むメッセージがキューに送信され、キューからピークされます。

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

using namespace System;
using namespace System::Messaging;

// This class represents an object the following example 
// sends to a queue and receives from a queue.
ref class Order
{
public:
   int orderId;
   DateTime orderTime;
};


/// <summary>
/// Provides a container class for the example.
/// </summary>
ref class MyNewQueue
{
public:

   //*************************************************
   // Posts a notification when a message arrives in 
   // the queue S"monitoredQueue". Does not retrieve any 
   // message information when peeking the message.
   //*************************************************
   void NotifyArrived()
   {
      // Connect to a queue.
      MessageQueue^ myQueue = gcnew MessageQueue( ".\\monitoredQueue" );

      // Specify to retrieve no message information.
      myQueue->MessageReadPropertyFilter->ClearAll();

      // Wait for a message to arrive. 
      Message^ emptyMessage = myQueue->Peek();

      // Post a notification when a message arrives.
      Console::WriteLine( "A message has arrived in the queue." );
      return;
   }


   //*************************************************
   // Sends an Order to a queue.
   //*************************************************
   void SendMessage()
   {
      // Create a new order and set values.
      Order^ sentOrder = gcnew Order;
      sentOrder->orderId = 3;
      sentOrder->orderTime = DateTime::Now;

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

      // Send the Order to the queue.
      myQueue->Send( sentOrder );
      return;
   }

   //*************************************************
   // Peeks a message containing an Order.
   //*************************************************
   void PeekFirstMessage()
   {
      // Connect to a queue.
      MessageQueue^ myQueue = gcnew MessageQueue( ".\\myQueue" );

      // Set the formatter to indicate the body contains an Order.
      array<Type^>^p = gcnew array<Type^>(1);
      p[ 0 ] = Order::typeid;
      myQueue->Formatter = gcnew XmlMessageFormatter( p );
      try
      {
         // Peek and format the message. 
         Message^ myMessage = myQueue->Peek();
         Order^ myOrder = static_cast<Order^>(myMessage->Body);

         // Display message information.
         Console::WriteLine( "Order ID: {0}", myOrder->orderId );
         Console::WriteLine( "Sent: {0}", myOrder->orderTime );
      }
      catch ( MessageQueueException^ ) 
      {
         // Handle Message Queuing exceptions.
      }
      // Handle invalid serialization format.
      catch ( InvalidOperationException^ e ) 
      {
         Console::WriteLine( e->Message );
      }

      // Catch other exceptions as necessary.
      return;
   }
};

//*************************************************
// Provides an entry point into the application.
//         
// This example posts a notification that a message
// has arrived in a queue. It sends a message 
// containing an other to a separate queue, and then
// peeks the first message in the queue.
//*************************************************
int main()
{
   // Create a new instance of the class.
   MyNewQueue^ myNewQueue = gcnew MyNewQueue;

   // Wait for a message to arrive in the queue.
   myNewQueue->NotifyArrived();

   // Send a message to a queue.
   myNewQueue->SendMessage();

   // Peek the first message in the queue.
   myNewQueue->PeekFirstMessage();
   return 0;
}
using System;
using System.Messaging;

namespace MyProject
{

    // This class represents an object the following example
    // sends to a queue and receives from a queue.
    public class Order
    {
        public int orderId;
        public DateTime orderTime;
    };	

    /// <summary>
    /// Provides a container class for the example.
    /// </summary>
    public class MyNewQueue
    {

        //**************************************************
        // Provides an entry point into the application.
        //		
        // This example posts a notification that a message
        // has arrived in a queue. It sends a message
        // containing an other to a separate queue, and then
        // peeks the first message in the queue.
        //**************************************************

        public static void Main()
        {
            // Create a new instance of the class.
            MyNewQueue myNewQueue = new MyNewQueue();

            // Wait for a message to arrive in the queue.
            myNewQueue.NotifyArrived();

            // Send a message to a queue.
            myNewQueue.SendMessage();	

            // Peek the first message in the queue.
            myNewQueue.PeekFirstMessage();
                        
            return;
        }

        //**************************************************
        // Posts a notification when a message arrives in
        // the queue "monitoredQueue". Does not retrieve any
        // message information when peeking the message.
        //**************************************************
        
        public void NotifyArrived()
        {

            // Connect to a queue.
            MessageQueue myQueue = new
                MessageQueue(".\\monitoredQueue");
    
            // Specify to retrieve no message information.
            myQueue.MessageReadPropertyFilter.ClearAll();

            // Wait for a message to arrive.
            Message emptyMessage = myQueue.Peek();

            // Post a notification when a message arrives.
            Console.WriteLine("A message has arrived in the queue.");

            return;
        }

        //**************************************************
        // Sends an Order to a queue.
        //**************************************************
        
        public void SendMessage()
        {
            
            // Create a new order and set values.
            Order sentOrder = new Order();
            sentOrder.orderId = 3;
            sentOrder.orderTime = DateTime.Now;

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

            // Send the Order to the queue.
            myQueue.Send(sentOrder);

            return;
        }

        //**************************************************
        // Peeks a message containing an Order.
        //**************************************************
        
        public void PeekFirstMessage()
        {
            // Connect to a queue.
            MessageQueue myQueue = new MessageQueue(".\\myQueue");
    
            // Set the formatter to indicate the body contains an Order.
            myQueue.Formatter = new XmlMessageFormatter(new Type[]
                {typeof(MyProject.Order)});
            
            try
            {
                // Peek and format the message.
                Message myMessage =	myQueue.Peek();
                Order myOrder = (Order)myMessage.Body;

                // Display message information.
                Console.WriteLine("Order ID: " +
                    myOrder.orderId.ToString());
                Console.WriteLine("Sent: " +
                    myOrder.orderTime.ToString());
            }
            
            catch (MessageQueueException)
            {
                // Handle Message Queuing exceptions.
            }

            // Handle invalid serialization format.
            catch (InvalidOperationException e)
            {
                Console.WriteLine(e.Message);
            }
            
            // Catch other exceptions as necessary.

            return;
        }
    }
}
Imports System.Messaging



    ' This class represents an object the following example 
    ' sends to a queue and peeks from a queue.
    Public Class Order
        Public orderId As Integer
        Public orderTime As DateTime
    End Class


   
    Public Class MyNewQueue


        
        ' Provides an entry point into the application.
        '		 
        ' This example posts a notification that a message
        ' has arrived in a queue. It sends a message 
        ' containing an other to a separate queue, and then
        ' peeks the first message in the queue.
        

        Public Shared Sub Main()

            ' Create a new instance of the class.
            Dim myNewQueue As New MyNewQueue()

            ' Wait for a message to arrive in the queue.
            myNewQueue.NotifyArrived()

            ' Send a message to a queue.
            myNewQueue.SendMessage()

            ' Peek the first message in the queue.
            myNewQueue.PeekFirstMessage()

            Return

        End Sub


        
        ' Posts a notification when a message arrives in 
        ' the queue "monitoredQueue". Does not retrieve any 
        ' message information when peeking the message.
        
        Public Sub NotifyArrived()

            ' Connect to a queue.
            Dim myQueue As New MessageQueue(".\monitoredQueue")

            ' Specify to retrieve no message information.
            myQueue.MessageReadPropertyFilter.ClearAll()

            ' Wait for a message to arrive. 
            Dim emptyMessage As Message = myQueue.Peek()

            ' Post a notification when a message arrives.
            Console.WriteLine("A message has arrived in the queue.")

            Return

        End Sub


        
        ' Sends an Order to a queue.
        

        Public Sub SendMessage()

            ' Create a new order and set values.
            Dim sentOrder As New Order()
            sentOrder.orderId = 3
            sentOrder.orderTime = DateTime.Now

            ' Connect to a queue on the local computer.
            Dim myQueue As New MessageQueue(".\myQueue")

            ' Send the Order to the queue.
            myQueue.Send(sentOrder)

            Return

        End Sub


        
        ' Peeks a message containing an Order.
        

        Public Sub PeekFirstMessage()

            ' Connect to a queue.
            Dim myQueue As New MessageQueue(".\myQueue")

            ' Set the formatter to indicate body contains an Order.
            myQueue.Formatter = New XmlMessageFormatter(New Type() _
                {GetType(Order)})

            Try

                ' Peek and format the message. 
                Dim myMessage As Message = myQueue.Peek()
                Dim myOrder As Order = CType(myMessage.Body, Order)

                ' Display message information.
                Console.WriteLine(("Order ID: " + _
                    myOrder.orderId.ToString()))
                Console.WriteLine(("Sent: " + _
                    myOrder.orderTime.ToString()))

            Catch m as MessageQueueException
                ' Handle Message Queuing exceptions.


            Catch e As InvalidOperationException
                ' Handle invalid serialization format.
                Console.WriteLine(e.Message)

                ' Catch other exceptions as necessary.

            End Try

            Return

        End Sub

End Class

注釈

このオーバーロードを使用して、キューをピークするか、キューにメッセージが存在するまで待機します。

メソッドは Peek キューから最初のメッセージを読み取りますが、削除しません。 したがって、優先順位の高いメッセージがキューに到着しない限り、同じメッセージを返す Peek 呼び出しが繰り返されます。 一方、 メソッドは Receive 、キューから最初のメッセージを読み取り、削除します。 そのため、 への呼び出しを Receive繰り返し実行すると、異なるメッセージが返されます。

メッセージ キューは、優先順位と到着時刻に従ってキュー内のメッセージを並べ替えます。 新しいメッセージは、優先度が高い場合にのみ、古いメッセージの前に配置されます。

キューにメッセージが到達するまで待機する間、現在のスレッドがブロックされてもいい場合は、Peek を使用します。 このオーバーロードではタイムアウトが指定されていないため、アプリケーションは無期限に待機する可能性があります。 待機せずにアプリケーションの処理を継続する必要がある場合は、非同期の BeginPeek メソッドを使用します。 または、タイムアウトを指定する のオーバーロード Peek を使用して、メッセージがキューに到着するタイムアウトを指定することもできます。

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

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

こちらもご覧ください

適用対象

Peek(TimeSpan)

この MessageQueue が参照するキューにある最初のメッセージを、削除せずに返します (ピークします)。 Peek() メソッドは同期メソッドであるため、メッセージが利用可能になるか、指定したタイムアウトが発生するまで、現在のスレッドをブロックします。

public:
 System::Messaging::Message ^ Peek(TimeSpan timeout);
public System.Messaging.Message Peek (TimeSpan timeout);
member this.Peek : TimeSpan -> System.Messaging.Message
Public Function Peek (timeout As TimeSpan) As Message

パラメーター

timeout
TimeSpan

キューにメッセージが格納されるまで待機する最大時間を示す TimeSpan

戻り値

キューの最初のメッセージを表す Message

例外

timeout パラメーターに指定した値が無効です。timeoutZero よりも小さいか、InfiniteTimeout よりも大きい可能性があります。

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

次のコード例では、Peekタイムアウトが 0 の メソッドを使用して、キューが空かどうかをチェックします。

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

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

   //*************************************************
   // Determines whether a queue is empty. The Peek()
   // method throws an exception if there is no message
   // in the queue. This method handles that exception 
   // by returning true to the calling method.
   //*************************************************
   bool IsQueueEmpty()
   {
      bool isQueueEmpty = false;
      
      // Connect to a queue.
      MessageQueue^ myQueue = gcnew MessageQueue( ".\\myQueue" );
      try
      {
         
         // Set Peek to return immediately.
         myQueue->Peek( TimeSpan(0) );
         
         // If an IOTime->Item[Out] was* not thrown, there is a message 
         // in the queue.
         isQueueEmpty = false;
      }
      catch ( MessageQueueException^ e ) 
      {
         if ( e->MessageQueueErrorCode == MessageQueueErrorCode::IOTimeout )
         {
            
            // No message was in the queue.
            isQueueEmpty = true;
         }

         
         // Handle other sources of MessageQueueException.
      }

      
      // Handle other exceptions as necessary.
      // Return true if there are no messages in the queue.
      return isQueueEmpty;
   }

};


//*************************************************
// Provides an entry point into the application.
//         
// This example determines whether a queue is empty.
//*************************************************
int main()
{
   
   // Create a new instance of the class.
   MyNewQueue^ myNewQueue = gcnew MyNewQueue;
   
   // Determine whether a queue is empty.
   bool isQueueEmpty = myNewQueue->IsQueueEmpty();
   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 determines whether a queue is empty.
        //**************************************************

        public static void Main()
        {
            // Create a new instance of the class.
            MyNewQueue myNewQueue = new MyNewQueue();

            // Determine whether a queue is empty.
            bool isQueueEmpty = myNewQueue.IsQueueEmpty();
                        
            return;
        }

        //**************************************************
        // Determines whether a queue is empty. The Peek()
        // method throws an exception if there is no message
        // in the queue. This method handles that exception
        // by returning true to the calling method.
        //**************************************************
        
        public bool IsQueueEmpty()
        {
            bool isQueueEmpty = false;

            // Connect to a queue.
            MessageQueue myQueue = new MessageQueue(".\\myQueue");

            try
            {
                // Set Peek to return immediately.
                myQueue.Peek(new TimeSpan(0));

                // If an IOTimeout was not thrown, there is a message
                // in the queue.
                isQueueEmpty = false;
            }

            catch(MessageQueueException e)
            {
                if (e.MessageQueueErrorCode ==
                    MessageQueueErrorCode.IOTimeout)
                {
                    // No message was in the queue.
                    isQueueEmpty = true;
                }

                // Handle other sources of MessageQueueException.
            }

            // Handle other exceptions as necessary.

            // Return true if there are no messages in the queue.
            return isQueueEmpty;
        }
    }
}
Imports System.Messaging



   
    Public Class MyNewQueue


        '
        ' Provides an entry point into the application.
        '		 
        ' This example determines whether a queue is empty.
        '

        Public Shared Sub Main()

            ' Create a new instance of the class.
            Dim myNewQueue As New MyNewQueue()

            ' Determine whether a queue is empty.
            Dim IsQueueEmpty As Boolean = myNewQueue.IsQueueEmpty()
        if IsQueueEMpty=True Then Console.WriteLine("Empty")
            

            Return

        End Sub


        '
        ' Determines whether a queue is empty. The Peek()
        ' method throws an exception if there is no message
        ' in the queue. This method handles that exception 
        ' by returning true to the calling method.
        '

        Public Function IsQueueEmpty() As Boolean

            'Dim QueueEmpty As Boolean = False

            ' Connect to a queue.
            Dim myQueue As New MessageQueue(".\myQueue")

            Try

                ' Set Peek to return immediately.
                myQueue.Peek(New TimeSpan(0))

                ' If an IOTimeout was not thrown, there is a message 
                ' in the queue.
                'queueEmpty = False

            Catch e As MessageQueueException

                If e.MessageQueueErrorCode = _
                    MessageQueueErrorCode.IOTimeout Then

                    ' No message was in the queue.
                    IsQueueEmpty = True

                End If

                ' Handle other sources of MessageQueueException as necessary.

                ' Handle other exceptions as necessary.

            End Try

            ' Return true if there are no messages in the queue.
            'Return queueEmpty
        IsQueueEmpty = False

        End Function 'IsQueueEmpty 

End Class

注釈

このオーバーロードを使用して、キューをピークするか、メッセージがキューに存在するまで指定された期間待機します。 メッセージがキューに既に存在する場合、メソッドは直ちに を返します。

メソッドは Peek キューから最初のメッセージを読み取りますが、削除しません。 したがって、優先順位の高いメッセージがキューに到着しない限り、同じメッセージを返す Peek 呼び出しが繰り返されます。 一方、 メソッドは Receive 、キューから最初のメッセージを読み取り、削除します。 そのため、 への呼び出しを Receive繰り返し実行すると、異なるメッセージが返されます。

メッセージ キューは、優先順位と到着時刻に従ってキュー内のメッセージを並べ替えます。 新しいメッセージは、優先度が高い場合にのみ、古いメッセージの前に配置されます。

キューにメッセージが到達するまで待機する間、現在のスレッドがブロックされてもいい場合は、Peek を使用します。 指定した期間まで、または を指定した場合は無期限にスレッドがブロックされます InfiniteTimeout。 待機せずにアプリケーションの処理を継続する必要がある場合は、非同期の BeginPeek メソッドを使用します。

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

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

こちらもご覧ください

適用対象

Peek(TimeSpan, Cursor, PeekAction)

指定されたカーソルを使用して、キュー内の現在のメッセージまたは次のメッセージを、削除せずに返します (ピークします)。 Peek() メソッドは同期メソッドであるため、メッセージが利用可能になるか、指定したタイムアウトが発生するまで、現在のスレッドをブロックします。

public:
 System::Messaging::Message ^ Peek(TimeSpan timeout, System::Messaging::Cursor ^ cursor, System::Messaging::PeekAction action);
public System.Messaging.Message Peek (TimeSpan timeout, System.Messaging.Cursor cursor, System.Messaging.PeekAction action);
member this.Peek : TimeSpan * System.Messaging.Cursor * System.Messaging.PeekAction -> System.Messaging.Message
Public Function Peek (timeout As TimeSpan, cursor As Cursor, action As PeekAction) As Message

パラメーター

timeout
TimeSpan

キューにメッセージが格納されるまで待機する最大時間を示す TimeSpan

cursor
Cursor

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

action
PeekAction

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

戻り値

キュー内のメッセージを表す Message

例外

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

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

timeout パラメーターに指定された値は無効です。 timeoutZero より小さい値か、InfiniteTimeout より大きい値である可能性があります。

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

注釈

このオーバーロードを使用して、キューをピークするか、メッセージがキューに存在するまで指定された期間待機します。 メッセージがキューに既に存在する場合、メソッドは直ちに を返します。

メソッドは Peek キューからメッセージを読み取りますが、削除しません。 一方、 メソッドは Receive 、キューからメッセージを読み取り、削除します。

キューにメッセージが到達するまで待機する間、現在のスレッドがブロックされてもいい場合は、Peek を使用します。 指定した期間まで、または を指定した場合は無期限にスレッドがブロックされます InfiniteTimeout。 待機せずにアプリケーションの処理を継続する必要がある場合は、非同期の BeginPeek メソッドを使用します。

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

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

こちらもご覧ください

適用対象

スレッド セーフ

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