次の方法で共有


MessageQueue.Peek メソッド

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

オーバーロードの一覧

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

[Visual Basic] Overloads Public Function Peek() As Message

[C#] public Message Peek();

[C++] public: Message* Peek();

[JScript] public function Peek() : Message;

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

[Visual Basic] Overloads Public Function Peek(TimeSpan) As Message

[C#] public Message Peek(TimeSpan);

[C++] public: Message* Peek(TimeSpan);

[JScript] public function Peek(TimeSpan) : Message;

使用例

[Visual Basic, C#, C++] タイムアウトが 0 の Peek メソッドを使用して、キューが空かどうかを確認する例を次に示します。

[Visual Basic, C#, C++] メモ   ここでは、Peek のオーバーロード形式のうちの 1 つだけについて、使用例を示します。その他の例については、各オーバーロード形式のトピックを参照してください。

 
Imports System
Imports 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 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()

            Return

        End Sub 'Main


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

        End Function 'IsQueueEmpty 

    End Class 'MyNewQueue
End Namespace 'MyProject

[C#] 
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;

        }
    }
}

[C++] 
#using <mscorlib.dll>
#using <system.dll>
#using <system.messaging.dll>

using namespace System;
using namespace System::Messaging;

__gc class MyNewQueue
{
    //*************************************************
    // 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(S".\\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 = new MyNewQueue();

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

    return 0;
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

参照

MessageQueue クラス | MessageQueue メンバ | System.Messaging 名前空間