次の方法で共有


MessageQueue.Receive メソッド

キューにある最初のメッセージを受信し、キューから削除します。

オーバーロードの一覧

MessageQueue で参照されるキューで利用できる最初のメッセージを受信します。この呼び出しは同期呼び出しであるため、メッセージが利用可能になるまで、現在のスレッドの実行をブロックします。

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

[C#] public Message Receive();

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

[JScript] public function Receive() : Message;

MessageQueue で参照されるトランザクション キューで利用できる最初のメッセージを受信します。この呼び出しは同期呼び出しであるため、メッセージが利用可能になるまで、現在のスレッドの実行をブロックします。

[Visual Basic] Overloads Public Function Receive(MessageQueueTransaction) As Message

[C#] public Message Receive(MessageQueueTransaction);

[C++] public: Message* Receive(MessageQueueTransaction*);

[JScript] public function Receive(MessageQueueTransaction) : Message;

MessageQueue で参照されるキューで利用できる最初のメッセージを受信します。この呼び出しは同期呼び出しであるため、メッセージが利用可能になるまで、現在のスレッドの実行をブロックします。

[Visual Basic] Overloads Public Function Receive(MessageQueueTransactionType) As Message

[C#] public Message Receive(MessageQueueTransactionType);

[C++] public: Message* Receive(MessageQueueTransactionType);

[JScript] public function Receive(MessageQueueTransactionType) : Message;

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

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

[C#] public Message Receive(TimeSpan);

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

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

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

[Visual Basic] Overloads Public Function Receive(TimeSpan, MessageQueueTransaction) As Message

[C#] public Message Receive(TimeSpan, MessageQueueTransaction);

[C++] public: Message* Receive(TimeSpan, MessageQueueTransaction*);

[JScript] public function Receive(TimeSpan, MessageQueueTransaction) : Message;

MessageQueue で参照されるキューで利用できる最初のメッセージを受信します。この呼び出しは同期的に実行され、メッセージがキューで使用できるようになるか、タイムアウトが経過するまで待機します。

[Visual Basic] Overloads Public Function Receive(TimeSpan, MessageQueueTransactionType) As Message

[C#] public Message Receive(TimeSpan, MessageQueueTransactionType);

[C++] public: Message* Receive(TimeSpan, MessageQueueTransactionType);

[JScript] public function Receive(TimeSpan, MessageQueueTransactionType) : Message;

使用例

[Visual Basic, C#, C++] メモ   ここでは、Receive のオーバーロード形式のうちの 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 sends and receives a message from
        ' a transactional queue.
        '**************************************************

        Public Shared Sub Main()

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

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

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

            Return

        End Sub 'Main


        '**************************************************
        ' Sends a message to a transactional queue.
        '**************************************************

        Public Sub SendMessageTransactional()

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

            ' Send a message to the queue.
            If myQueue.Transactional = True Then

                myQueue.Send("My Message Data.", New _
                    MessageQueueTransaction())

            End If

            Return

        End Sub 'SendMessageTransactional


        '**************************************************
        ' Receives a message from the transactional queue.
        '**************************************************

        Public Sub ReceiveMessageTransactional()

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

            ' Set the formatter.
            myQueue.Formatter = New XmlMessageFormatter(New Type() _
                {GetType([String])})

            ' Create a transaction.
            Dim myTransaction As New MessageQueueTransaction()

            Try

                ' Begin the transaction.
                myTransaction.Begin()

                ' Receive the message. 
                ' Wait five seconds for a message to arrive. 
                Dim myMessage As Message = myQueue.Receive(New _
                    TimeSpan(0, 0, 5), myTransaction)
                Dim myOrder As [String] = CType(myMessage.Body, _
                    [String])

                ' Display message information.
                Console.WriteLine(myOrder)

                ' Commit the transaction.
                myTransaction.Commit()


            Catch e As MessageQueueException

                ' Handle nontransactional queues.
                If e.MessageQueueErrorCode = _
                    MessageQueueErrorCode.TransactionUsage Then

                    Console.WriteLine("Queue is not transactional.")

                Else
                    ' Handle no message arriving in the queue.
                    If e.MessageQueueErrorCode = _
                        MessageQueueErrorCode.IOTimeout Then

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

                    End If
                End If

                ' Else catch other sources of a MessageQueueException.

                ' Roll back the transaction.
                myTransaction.Abort()


                ' Catch other exceptions as necessary, such as InvalidOperationException,
                ' thrown when the formatter cannot deserialize the message.

            End Try

            Return

        End Sub 'ReceiveMessageTransactional

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

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

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

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

            return;
        }


        //**************************************************
        // Sends a message to a transactional queue.
        //**************************************************
        
        public void SendMessageTransactional()
        {
                        
            // Connect to a queue on the local computer.
            MessageQueue myQueue = new 
                MessageQueue(".\\myTransactionalQueue");

            // Send a message to the queue.
            if (myQueue.Transactional == true)
            {
                myQueue.Send("My Message Data.", new 
                    MessageQueueTransaction());
            }

            return;
        }


        //**************************************************
        // Receives a message from the transactional queue.
        //**************************************************
        
        public  void ReceiveMessageTransactional()
        {
            // Connect to a transactional queue on the local computer.
            MessageQueue myQueue = new 
                MessageQueue(".\\myTransactionalQueue");

            // Set the formatter.
            myQueue.Formatter = new XmlMessageFormatter(new Type[]
                {typeof(String)});
            
            // Create a transaction.
            MessageQueueTransaction myTransaction = new 
                MessageQueueTransaction();

            try
            {
                // Begin the transaction.
                myTransaction.Begin();
                
                // Receive the message. 
                // Wait five seconds for a message to arrive. 
                Message myMessage =    myQueue.Receive(new 
                    TimeSpan(0,0,5), myTransaction); 
                
                String myOrder = (String)myMessage.Body;

                // Display message information.
                Console.WriteLine(myOrder);

                // Commit the transaction.
                myTransaction.Commit();

            }
            
            catch (MessageQueueException e)
            {
                // Handle nontransactional queues.
                if (e.MessageQueueErrorCode == 
                    MessageQueueErrorCode.TransactionUsage)
                { 
                    Console.WriteLine("Queue is not transactional.");
                }

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

                // Roll back the transaction.
                myTransaction.Abort();
            }

            // Catch other exceptions as necessary, such as 
            // InvalidOperationException, thrown when the formatter 
            // cannot deserialize the message.

            return;
        }
    }
}

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

using namespace System;
using namespace System::Messaging;

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

        // Send a message to the queue.
        if (myQueue->Transactional == true) 
        {
            myQueue->Send(S"My Message Data.", new MessageQueueTransaction());
        }

        return;
    }

    //*************************************************
    // Receives a message from the transactional queue.
    //*************************************************
public:
    void ReceiveMessageTransactional() 
    {
        // Connect to a transactional queue on the local computer.
        MessageQueue* myQueue = new MessageQueue(S".\\myTransactionalQueue");

        // Set the formatter.
        Type* p __gc[] = new Type* __gc[1];
        p[0] = __typeof(String);
        myQueue->Formatter = new XmlMessageFormatter( p );

        // Create a transaction.
        MessageQueueTransaction* myTransaction = new MessageQueueTransaction();

        try 
        {
            // Begin the transaction.
            myTransaction->Begin();

            // Receive the message. 
            // Wait five seconds for a message to arrive. 
            Message* myMessage = myQueue->Receive(TimeSpan(0, 0, 5), myTransaction); 

            String* myOrder = static_cast<String*>(myMessage->Body);

            // Display message information.
            Console::WriteLine(myOrder);

            // Commit the transaction.
            myTransaction->Commit();

        } 
        catch (MessageQueueException* e) 
        {
            // Handle nontransactional queues.
            if (e->MessageQueueErrorCode == 
                MessageQueueErrorCode::TransactionUsage) 
            { 
                Console::WriteLine(S"Queue is not transactional.");
            }

            // Handle no message arriving in the queue.
            else if (e->MessageQueueErrorCode == 
                MessageQueueErrorCode::IOTimeout) 
            {
                Console::WriteLine(S"No message in queue.");
            }

            // Else catch other sources of MessageQueueException.

            // Roll back the transaction.
            myTransaction->Abort();
        }

        // Catch other exceptions as necessary, such as 
        // InvalidOperationException, thrown when the formatter 
        // cannot deserialize the message.

        return;
    }
};

//*************************************************
// Provides an entry point into the application.
// 
// This example sends and receives a message from
// a transactional queue.
//*************************************************

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

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

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

    return 0;
}

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

参照

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