MessageQueue.Receive メソッド (TimeSpan)

MessageQueue が参照するキューで利用できる最初のメッセージを受信します。キューでメッセージが利用可能になるか、タイムアウトが経過するまで待機します。

Overloads Public Function Receive( _
   ByVal timeout As TimeSpan _) As Message
[C#]
public Message Receive(TimeSpantimeout);
[C++]
public: Message* Receive(TimeSpantimeout);
[JScript]
public function Receive(
   timeout : TimeSpan) : Message;

パラメータ

  • timeout
    新しいメッセージを検査できるようになるまでの待機時間を示す TimeSpan

戻り値

キューで利用できる最初のメッセージを参照する Message

例外

例外の種類 条件
ArgumentException timeout パラメータに指定した値が不正です。 timeoutTimeSpan.Zero よりも小さいか、 TimeSpan.MaxValue よりも大きい可能性があります。
MessageQueueException タイムアウトが経過する前に、キューにメッセージが到達しませんでした。

または

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

解説

このオーバーロードを使用して、キューからメッセージを受信します。キューにメッセージがない場合は、指定した時間が経過した後で制御が戻ります。

Receive メソッドを使用すると、メッセージを同期的に読み取り、キューから削除できます。後続の Receive 呼び出しは、キューにある次のメッセージを返します。これは、新しいメッセージか、または優先順位の高いメッセージです。

キューにある最初のメッセージをキューから削除せずに読み取るには、 Peek メソッドを使用します。 Peek メソッドは、常にキューの最初のメッセージを返します。そのため、より優先順位の高いメッセージがそのキューに到達するまで、後続の呼び出しでも同じメッセージが返されます。

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

このメソッドが各種のワークグループ モードで使用できるかどうかを次の表に示します。

ワークグループ モード 使用可否
ローカル コンピュータ はい
ローカル コンピュータ + 直接書式名 はい
リモート コンピュータ いいえ
リモート コンピュータ + 直接書式名 はい

使用例

[Visual Basic, C#, C++] キューからメッセージを受信し、そのメッセージに関する情報を画面に出力する例を次に示します。この例では、メッセージのキューへの到着を待つ間、実行が最大 5 秒間一時停止されます。

 
Imports System
Imports System.Messaging

Namespace MyProject

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


    '/ <summary>
    '/ Provides a container class for the example.
    '/ </summary>
    Public Class MyNewQueue


        '**************************************************
        ' Provides an entry point into the application.
        '         
        ' This example receives a message from a queue.
        '**************************************************

        Public Shared Sub Main()

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

            ' Receive a message from a queue.
            myNewQueue.ReceiveMessage()

            Return

        End Sub 'Main


        '**************************************************
        ' Receives a message containing an Order.
        '**************************************************

        Public Sub ReceiveMessage()

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

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

            Try

                ' Receive and format the message. 
                ' Wait 5 seconds for a message to arrive.
                Dim myMessage As Message = myQueue.Receive(New _
                    TimeSpan(0, 0, 5))
                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 e As MessageQueueException
                ' Handle no message arriving in the queue.
                If e.MessageQueueErrorCode = _
                    MessageQueueErrorCode.IOTimeout Then

                    Console.WriteLine("No message arrived in queue.")

                End If

                ' Handle other sources of a MessageQueueException.

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

                ' Catch other exceptions as necessary.

            End Try

            Return

        End Sub 'ReceiveMessage

    End Class 'MyNewQueue
End Namespace 'MyProject

[C#] 
using System;
using System.Messaging;

namespace MyProject
{
    // This class represents an object the following example 
    // 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 receives a message from a queue.
        //**************************************************

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

            // Receive a message from a queue.
            myNewQueue.ReceiveMessage();

            return;
        }


        //**************************************************
        // Receives a message containing an Order.
        //**************************************************

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

            // Set the formatter to indicate body contains an Order.
            myQueue.Formatter = new XmlMessageFormatter(new Type[]
                {typeof(MyProject.Order)});
            
            try
            {
                // Receive and format the message. 
                // Wait 5 seconds for a message to arrive.
                Message myMessage =    myQueue.Receive(new 
                    TimeSpan(0,0,5)); 
                Order myOrder = (Order)myMessage.Body;

                // Display message information.
                Console.WriteLine("Order ID: " + 
                    myOrder.orderId.ToString());
                Console.WriteLine("Sent: " + 
                    myOrder.orderTime.ToString());
            }

            catch (MessageQueueException e)
            {
                // Handle no message arriving in the queue.
                if (e.MessageQueueErrorCode == 
                    MessageQueueErrorCode.IOTimeout)
                {
                    Console.WriteLine("No message arrived in queue.");
                }            

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

            return;
        }
    }
}

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

using namespace System;
using namespace System::Messaging;

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

/// <summary>
/// Provides a container class for the example.
/// </summary>
__gc class MyNewQueue 
{
public:
    //*************************************************
    // Receives a message containing an Order.
    //*************************************************
    void ReceiveMessage() 
    {
        // Connect to the a queue on the local computer.
        MessageQueue* myQueue = new MessageQueue(S".\\myQueue");

        // Set the formatter to indicate body contains an Order.
        Type* p __gc[] = new Type* __gc[1];
        p[0] = __typeof(Order);
        myQueue->Formatter = new XmlMessageFormatter( p );

        try 
        {
            // Receive and format the message. 
            // Wait 5 seconds for a message to arrive.
            Message* myMessage = myQueue->Receive(TimeSpan(0, 0, 5)); 
            Order* myOrder = static_cast<Order*>(myMessage->Body);

            // Display message information.
            Console::WriteLine(S"Order ID: {0}", __box(myOrder->orderId));
            Console::WriteLine(S"Sent: {0}", __box(myOrder->orderTime));
        }
        catch (MessageQueueException* e) 
        {
            // Handle no message arriving in the queue.
            if (e->MessageQueueErrorCode == 
                MessageQueueErrorCode::IOTimeout) 
            {
                Console::WriteLine(S"No message arrived in queue.");
            }            

            // Handle other sources of a MessageQueueException.
        }

        // 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 receives a message from a queue.
//*************************************************

int main() 
{
    // Create a new instance of the class.
    MyNewQueue* myNewQueue = new MyNewQueue();

    // Receive a message from a queue.
    myNewQueue->ReceiveMessage();

    return 0;
}

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

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

.NET Framework セキュリティ:

参照

MessageQueue クラス | MessageQueue メンバ | System.Messaging 名前空間 | MessageQueue.Receive オーバーロードの一覧 | InfiniteTimeout | ReceiveById | ReceiveByCorrelationId | Peek | BeginReceive