MessageQueue.Receive メソッド

定義

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

オーバーロード

Receive()

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

Receive(MessageQueueTransaction)

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

Receive(MessageQueueTransactionType)

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

Receive(TimeSpan)

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

Receive(TimeSpan, Cursor)

指定したカーソルを使用して、キューの現在のメッセージを受信します。 利用可能なメッセージがない場合、このメソッドは、メッセージが利用可能になるまで、またはタイムアウトが発生するまで待機します。

Receive(TimeSpan, MessageQueueTransaction)

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

Receive(TimeSpan, MessageQueueTransactionType)

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

Receive(TimeSpan, Cursor, MessageQueueTransaction)

指定したカーソルを使用して、キューの現在のメッセージを受信します。 利用可能なメッセージがない場合、このメソッドは、メッセージが利用可能になるまで、またはタイムアウトが発生するまで待機します。

Receive(TimeSpan, Cursor, MessageQueueTransactionType)

指定したカーソルを使用して、キューの現在のメッセージを受信します。 利用可能なメッセージがない場合、このメソッドは、メッセージが利用可能になるまで、またはタイムアウトが発生するまで待機します。

Receive()

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

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

戻り値

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

例外

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

次のコード例では、キューからメッセージを受信し、そのメッセージに関する情報を画面に出力します。

#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:

   //*************************************************
   // 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;
   }

   //*************************************************
   // Receives a message containing an Order.
   //*************************************************
   void ReceiveMessage()
   {
      // Connect to the a queue on the local computer.
      MessageQueue^ myQueue = gcnew MessageQueue( ".\\myQueue" );

      // Set the formatter to indicate body contains an Order.
      array<Type^>^p = gcnew array<Type^>(1);
      p[ 0 ] = Order::typeid;
      myQueue->Formatter = gcnew XmlMessageFormatter( p );
      try
      {
         // Receive and format the message. 
         Message^ myMessage = myQueue->Receive();
         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 sends and receives a message from
// a queue.
//*************************************************
int main()
{
   // Create a new instance of the class.
   MyNewQueue^ myNewQueue = gcnew MyNewQueue;

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

   // Receive a message from a queue.
   myNewQueue->ReceiveMessage();
   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 sends and receives a message from
        // a queue.
        //**************************************************

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

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

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

            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;
        }

        //**************************************************
        // 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.
                Message myMessage =	myQueue.Receive();
                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 receives 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 sends and receives a message from
        ' a qeue.
        '

        Public Shared Sub Main()

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

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

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

            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


        '
        ' 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 the body contains an Order.
            myQueue.Formatter = New XmlMessageFormatter(New Type() _
                {GetType(Order)})

            Try

                ' Receive and format the message. 
                Dim myMessage As Message = myQueue.Receive()
                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

注釈

このオーバーロードを使用して、キューからメッセージを受信するか、キューにメッセージが含まれるまで待機します。

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

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

メッセージがキューに到着するのを Receive 待機している間に、現在のスレッドがブロックされる可能性がある場合は、 の呼び出しを使用します。 メソッドのこのオーバーロードは Receive 無限のタイムアウトを指定するため、アプリケーションは無期限に待機する可能性があります。 メッセージを待たずにアプリケーションの処理を続行する必要がある場合は、非同期メソッド BeginReceiveの 使用を検討してください。

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

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

こちらもご覧ください

適用対象

Receive(MessageQueueTransaction)

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

public:
 System::Messaging::Message ^ Receive(System::Messaging::MessageQueueTransaction ^ transaction);
public System.Messaging.Message Receive (System.Messaging.MessageQueueTransaction transaction);
member this.Receive : System.Messaging.MessageQueueTransaction -> System.Messaging.Message
Public Function Receive (transaction As MessageQueueTransaction) As Message

パラメーター

transaction
MessageQueueTransaction

MessageQueueTransaction オブジェクト。

戻り値

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

例外

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

- または -

キューが非トランザクション キューです。

次のコード例では、ローカル コンピューター上のトランザクション キューに接続し、キューにメッセージを送信します。 その後、注文を含むメッセージを受信します。 非トランザクション キューが発生すると、トランザクションがスローされ、例外が発生し、トランザクションがロールバックされます。

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

using namespace System;
using namespace System::Messaging;

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

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

      // Send a message to the queue.
      if ( myQueue->Transactional == true )
      {
         // Create a transaction.
         MessageQueueTransaction^ myTransaction = gcnew MessageQueueTransaction;

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

         // Send the message.
         myQueue->Send( "My Message Data.", myTransaction );

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

      return;
   }


   //*************************************************
   // Receives a message containing an Order.
   //*************************************************
   void ReceiveMessageTransactional()
   {
      // Connect to a transactional queue on the local computer.
      MessageQueue^ myQueue = gcnew MessageQueue( ".\\myTransactionalQueue" );

      // Set the formatter.
      array<Type^>^p = gcnew array<Type^>(1);
      p[ 0 ] = String::typeid;
      myQueue->Formatter = gcnew XmlMessageFormatter( p );

      // Create a transaction.
      MessageQueueTransaction^ myTransaction = gcnew MessageQueueTransaction;
      try
      {
         // Begin the transaction.
         myTransaction->Begin();

         // Receive the message. 
         Message^ myMessage = myQueue->Receive( 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( "Queue is not transactional." );
         }

         // 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.
      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 = gcnew MyNewQueue;

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

   // Receive a message from a queue.
   myNewQueue->ReceiveMessageTransactional();
   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 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 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)
            {
                // Create a transaction.
                MessageQueueTransaction myTransaction = new
                    MessageQueueTransaction();

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

                // Send the message.
                myQueue.Send("My Message Data.", myTransaction);

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

            return;
        }

        //**************************************************
        // Receives a message containing an Order.
        //**************************************************
        
        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.
                Message myMessage =	myQueue.Receive(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.");
                }
                
                // 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.

            return;
        }
    }
}
Imports System.Messaging

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


        '
        ' Sends a message to a 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

                ' Create a transaction.
                Dim myTransaction As New MessageQueueTransaction

                ' Begin the transaction.
                myTransaction.Begin()

                ' Send the message.
                myQueue.Send("My Message Data.", myTransaction)

                ' Commit the transaction.
                myTransaction.Commit()

            End If

            Return

        End Sub


        '
        ' Receives a message containing an Order.
        '

        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. 
                Dim myMessage As Message = _
                    myQueue.Receive(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.")

                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

End Class

注釈

パラメーターで定義された transaction 内部トランザクション コンテキストを使用してトランザクション キューからメッセージを受信する場合、またはキューにメッセージが含まれるまで待機するには、このオーバーロードを使用します。

メソッドを Receive 使用すると、メッセージを同期的に読み取り、キューからメッセージを削除できます。 以降の を Receive 呼び出すと、キュー内の後続のメッセージが返されます。

このメソッドはトランザクション キューで呼び出されるため、トランザクションが中止されると、受信したメッセージがキューに返されます。 トランザクションがコミットされるまで、メッセージはキューから完全に削除されません。

キューから削除せずにキュー内の最初のメッセージを読み取る場合は、 メソッドを使用します Peek 。 メソッドは Peek 常にキュー内の最初のメッセージを返します。そのため、 メソッドの後続の呼び出しでは、優先順位の高いメッセージがキューに到着しない限り、同じメッセージが返されます。 への Peek呼び出しによって返されるメッセージに関連付けられたトランザクション コンテキストはありません。 はキュー内のメッセージを削除しないため Peek 、 の Abort呼び出しによってロールバックするものは何もありません。

メッセージがキューに到着するのを Receive 待機している間に、現在のスレッドがブロックされる可能性がある場合は、 の呼び出しを使用します。 メソッドのこのオーバーロードは Receive 無限のタイムアウトを指定するため、アプリケーションは無期限に待機する可能性があります。 メッセージを待たずにアプリケーションの処理を続行する必要がある場合は、非同期メソッド BeginReceiveの 使用を検討してください。

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

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

こちらもご覧ください

適用対象

Receive(MessageQueueTransactionType)

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

public:
 System::Messaging::Message ^ Receive(System::Messaging::MessageQueueTransactionType transactionType);
public System.Messaging.Message Receive (System.Messaging.MessageQueueTransactionType transactionType);
member this.Receive : System.Messaging.MessageQueueTransactionType -> System.Messaging.Message
Public Function Receive (transactionType As MessageQueueTransactionType) As Message

パラメーター

transactionType
MessageQueueTransactionType

メッセージと関連付けるトランザクション コンテキストの種類を示す、MessageQueueTransactionType 値の 1 つ。

戻り値

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

例外

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

transactionType パラメーターが、MessageQueueTransactionType メンバーの 1 つではありません。

次のコード例は、Receive(MessageQueueTransactionType) の使用方法を示します。


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

// Create a new message.
Message^ msg = gcnew Message("Example Message Body");

// Send the message.
queue->Send(msg, MessageQueueTransactionType::Single);

// Simulate doing other work so the message has time to arrive.
System::Threading::Thread::Sleep(TimeSpan::FromSeconds(10.0));

// Set the formatter to indicate the message body contains a String.
queue->Formatter = gcnew XmlMessageFormatter(
    gcnew array<Type^>{String::typeid});

// Receive the message from the queue.  Because the Id of the message
// , it might not be the message just sent.
msg = queue->Receive(MessageQueueTransactionType::Single);

queue->Close();

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

// Create a new message.
Message msg = new Message("Example Message Body");

// Send the message.
queue.Send(msg, MessageQueueTransactionType.Single);

// Simulate doing other work so the message has time to arrive.
System.Threading.Thread.Sleep(TimeSpan.FromSeconds(10.0));

// Set the formatter to indicate the message body contains a String.
queue.Formatter = new XmlMessageFormatter(new Type[]
    {typeof(String)});

// Receive the message from the queue.  Because the Id of the message
// , it might not be the message just sent.
msg = queue.Receive(MessageQueueTransactionType.Single);

注釈

パラメーターで定義された transactionType トランザクション コンテキストを使用してキューからメッセージを受信するか、キューにメッセージが存在するまで待機するには、このオーバーロードを使用します。

メッセージの受信に使用するtransactionType外部トランザクション コンテキストがスレッドに既にアタッチされている場合は、 パラメーターに を指定Automaticします。 メッセージを 1 つの内部トランザクションとして受信するかどうかを指定 Single します。 トランザクション コンテキスト以外のトランザクション キューからメッセージを受信するかどうかを指定 None できます。

メソッドを Receive 使用すると、メッセージを同期的に読み取り、キューからメッセージを削除できます。 以降の を Receive 呼び出すと、キュー内の後続のメッセージが返されます。

トランザクション キューからメッセージを受信するためにこのメソッドが呼び出された場合、トランザクションが中止されると、受信したメッセージがキューに返されます。 トランザクションがコミットされるまで、メッセージはキューから完全に削除されません。

キューから削除せずにキュー内の最初のメッセージを読み取る場合は、 メソッドを使用します Peek 。 メソッドは Peek 常にキュー内の最初のメッセージを返すので、優先順位の高いメッセージがキューに到着しない限り、メソッドの後続の呼び出しでは同じメッセージが返されます。 への Peek呼び出しによって返されるメッセージに関連付けられたトランザクション コンテキストはありません。 はキュー内のメッセージを削除しないため Peek 、 の Abort呼び出しによってロールバックするものは何もありません。

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

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

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

こちらもご覧ください

適用対象

Receive(TimeSpan)

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

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

パラメーター

timeout
TimeSpan

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

戻り値

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

例外

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

タイムアウトが経過する前に、キューにメッセージが到達しませんでした。

- または -

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

次のコード例では、キューからメッセージを受信し、そのメッセージに関する情報を画面に出力します。 この例では、キューにメッセージが到着するのを待っている間、実行を最大 5 秒間一時停止します。

#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.
ref class Order
{
public:
   int orderId;
   DateTime orderTime;
};


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

   //*************************************************
   // Receives a message containing an Order.
   //*************************************************
   void ReceiveMessage()
   {
      // Connect to the a queue on the local computer.
      MessageQueue^ myQueue = gcnew MessageQueue( ".\\myQueue" );

      // Set the formatter to indicate body contains an Order.
      array<Type^>^p = gcnew array<Type^>(1);
      p[ 0 ] = Order::typeid;
      myQueue->Formatter = gcnew 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( "Order ID: {0}", myOrder->orderId );
         Console::WriteLine( "Sent: {0}", myOrder->orderTime );
      }
      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;
   }
};

//*************************************************
// 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 = gcnew MyNewQueue;

   // Receive a message from a queue.
   myNewQueue->ReceiveMessage();
   return 0;
}
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;
        }
    }
}
Imports System.Messaging

' 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


   
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


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

End Class

注釈

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

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

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

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

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

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

こちらもご覧ください

適用対象

Receive(TimeSpan, Cursor)

指定したカーソルを使用して、キューの現在のメッセージを受信します。 利用可能なメッセージがない場合、このメソッドは、メッセージが利用可能になるまで、またはタイムアウトが発生するまで待機します。

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

パラメーター

timeout
TimeSpan

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

cursor
Cursor

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

戻り値

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

例外

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

タイムアウトが経過する前に、キューにメッセージが到達しませんでした。

- または -

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

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

適用対象

Receive(TimeSpan, MessageQueueTransaction)

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

public:
 System::Messaging::Message ^ Receive(TimeSpan timeout, System::Messaging::MessageQueueTransaction ^ transaction);
public System.Messaging.Message Receive (TimeSpan timeout, System.Messaging.MessageQueueTransaction transaction);
member this.Receive : TimeSpan * System.Messaging.MessageQueueTransaction -> System.Messaging.Message
Public Function Receive (timeout As TimeSpan, transaction As MessageQueueTransaction) As Message

パラメーター

timeout
TimeSpan

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

transaction
MessageQueueTransaction

MessageQueueTransaction オブジェクト。

戻り値

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

例外

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

タイムアウトが経過する前に、キューにメッセージが到達しませんでした。

- または -

キューが非トランザクション キューです。

- または -

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

次のコード例は、このメソッドの使用方法を示しています。

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

using namespace System;
using namespace System::Messaging;

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

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

      // Send a message to the queue.
      if ( myQueue->Transactional == true )
      {
         // Create a transaction.
         MessageQueueTransaction^ myTransaction = gcnew MessageQueueTransaction;

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

         // Send the message.
         myQueue->Send( "My Message Data.", myTransaction );

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

      return;
   }

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

      // Set the formatter.
      array<Type^>^p = gcnew array<Type^>(1);
      p[ 0 ] = String::typeid;
      myQueue->Formatter = gcnew XmlMessageFormatter( p );

      // Create a transaction.
      MessageQueueTransaction^ myTransaction = gcnew 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( "Queue is not transactional." );
         }
         // Handle no message arriving in the queue.
         else

         // Handle no message arriving in the queue.
         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;
   }
};

//*************************************************
// 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 = gcnew MyNewQueue;

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

   // Receive a message from a queue.
   myNewQueue->ReceiveMessageTransactional();
   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 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)
            {
                // Create a transaction.
                MessageQueueTransaction myTransaction = new
                    MessageQueueTransaction();

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

                // Send the message.
                myQueue.Send("My Message Data.", myTransaction);

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

            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;
        }
    }
}
Imports System.Messaging

Namespace MyProj


   
    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


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

                ' Create a transaction.
                Dim myTransaction As New MessageQueueTransaction

                ' Begin the transaction.
                myTransaction.Begin()

                ' Send the message.
                myQueue.Send("My Message Data.", myTransaction)

                ' Commit the transaction.
                myTransaction.Commit()

            End If

            Return

        End Sub


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

    End Class
End Namespace 'MyProj

注釈

パラメーターで定義された transaction 内部トランザクション コンテキストを使用してトランザクション キューからメッセージを受信し、キューにメッセージがない場合は、指定した期間内にを返すには、このオーバーロードを使用します。

メソッドを Receive 使用すると、メッセージを同期的に読み取り、キューからメッセージを削除できます。 以降の を Receive 呼び出すと、キュー内の後続のメッセージが返されます。

このメソッドはトランザクション キューで呼び出されるため、トランザクションが中止されると、受信したメッセージがキューに返されます。 トランザクションがコミットされるまで、メッセージはキューから完全に削除されません。

キューから削除せずにキュー内の最初のメッセージを読み取る場合は、 メソッドを使用します Peek 。 メソッドは Peek 常にキュー内の最初のメッセージを返すので、優先順位の高いメッセージがキューに到着しない限り、メソッドの後続の呼び出しでは同じメッセージが返されます。 への Peek呼び出しによって返されるメッセージに関連付けられたトランザクション コンテキストはありません。 はキュー内のメッセージを削除しないため Peek 、 の Abort呼び出しによってロールバックするものは何もありません。

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

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

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

こちらもご覧ください

適用対象

Receive(TimeSpan, MessageQueueTransactionType)

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

public:
 System::Messaging::Message ^ Receive(TimeSpan timeout, System::Messaging::MessageQueueTransactionType transactionType);
public System.Messaging.Message Receive (TimeSpan timeout, System.Messaging.MessageQueueTransactionType transactionType);
member this.Receive : TimeSpan * System.Messaging.MessageQueueTransactionType -> System.Messaging.Message
Public Function Receive (timeout As TimeSpan, transactionType As MessageQueueTransactionType) As Message

パラメーター

timeout
TimeSpan

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

transactionType
MessageQueueTransactionType

メッセージと関連付けるトランザクション コンテキストの種類を示す、MessageQueueTransactionType 値の 1 つ。

戻り値

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

例外

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

transactionType パラメーターが、MessageQueueTransactionType メンバーの 1 つではありません。

タイムアウトが経過する前に、キューにメッセージが到達しませんでした。

- または -

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

次のコード例は、このメソッドの使用方法を示しています。


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

// Create a new message.
Message^ msg = gcnew Message("Example Message Body");

// Send the message.
queue->Send(msg, MessageQueueTransactionType::Single);

// Set the formatter to indicate the message body contains a String.
queue->Formatter = gcnew XmlMessageFormatter(
    gcnew array<Type^>{String::typeid});

// Receive the message from the queue. Because the Id of the message
// is not specified, it might not be the message just sent.
msg = queue->Receive(TimeSpan::FromSeconds(10.0),
    MessageQueueTransactionType::Single);

queue->Close();

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

// Create a new message.
Message msg = new Message("Example Message Body");

// Send the message.
queue.Send(msg, MessageQueueTransactionType.Single);

// Set the formatter to indicate the message body contains a String.
queue.Formatter = new XmlMessageFormatter(new Type[]
    {typeof(String)});

// Receive the message from the queue. Because the Id of the message
// is not specified, it might not be the message just sent.
msg = queue.Receive(TimeSpan.FromSeconds(10.0),
    MessageQueueTransactionType.Single);

注釈

このオーバーロードを使用して、 パラメーターで定義されたトランザクション コンテキストを使用してキューからメッセージを transactionType 受信し、キューにメッセージがない場合は、指定した期間内にを返します。

メッセージの受信に使用するtransactionTypeスレッドに外部トランザクション コンテキストが既にアタッチされている場合は、 パラメーターに を指定Automaticします。 メッセージを 1 つの内部トランザクションとして受信するかどうかを指定 Single します。 トランザクション コンテキストの外部のトランザクション キューからメッセージを受信するかどうかを指定 None できます。

メソッドを Receive 使用すると、メッセージを同期的に読み取り、キューからメッセージを削除できます。 以降の を Receive 呼び出すと、キュー内の後続のメッセージが返されます。

トランザクション キューからメッセージを受信するためにこのメソッドが呼び出された場合、トランザクションが中止されると、受信したメッセージがキューに返されます。 トランザクションがコミットされるまで、メッセージはキューから完全に削除されません。

キューから削除せずにキュー内の最初のメッセージを読み取る場合は、 メソッドを使用します Peek 。 メソッドは Peek 常にキュー内の最初のメッセージを返すので、優先順位の高いメッセージがキューに到着しない限り、メソッドの後続の呼び出しでは同じメッセージが返されます。 への Peek呼び出しによって返されるメッセージに関連付けられたトランザクション コンテキストはありません。 はキュー内のメッセージを削除しないため Peek 、 の Abort呼び出しによってロールバックするものは何もありません。

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

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

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

こちらもご覧ください

適用対象

Receive(TimeSpan, Cursor, MessageQueueTransaction)

指定したカーソルを使用して、キューの現在のメッセージを受信します。 利用可能なメッセージがない場合、このメソッドは、メッセージが利用可能になるまで、またはタイムアウトが発生するまで待機します。

public:
 System::Messaging::Message ^ Receive(TimeSpan timeout, System::Messaging::Cursor ^ cursor, System::Messaging::MessageQueueTransaction ^ transaction);
public System.Messaging.Message Receive (TimeSpan timeout, System.Messaging.Cursor cursor, System.Messaging.MessageQueueTransaction transaction);
member this.Receive : TimeSpan * System.Messaging.Cursor * System.Messaging.MessageQueueTransaction -> System.Messaging.Message
Public Function Receive (timeout As TimeSpan, cursor As Cursor, transaction As MessageQueueTransaction) As Message

パラメーター

timeout
TimeSpan

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

cursor
Cursor

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

transaction
MessageQueueTransaction

MessageQueueTransaction オブジェクト。

戻り値

キューのメッセージを参照する Message

例外

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

または

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

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

タイムアウトが経過する前に、キューにメッセージが到達しませんでした。

- または -

キューが非トランザクション キューです。

- または -

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

注釈

パラメーターで定義された transaction 内部トランザクション コンテキストを使用してトランザクション キューからメッセージを受信し、キューにメッセージがない場合は、指定した期間内にを返すには、このオーバーロードを使用します。

メソッドを Receive 使用すると、メッセージを同期的に読み取り、キューからメッセージを削除できます。 キュー内 Receive の後続のメッセージを返す後続の呼び出し。

このメソッドはトランザクション キューで呼び出されるため、トランザクションが中止されると、受信したメッセージがキューに返されます。 トランザクションがコミットされるまで、メッセージはキューから完全に削除されません。

キューからメッセージを削除せずにキュー内のメッセージを読み取る場合は、 メソッドを使用します Peek 。 への Peek呼び出しによって返されるメッセージに関連付けられたトランザクション コンテキストはありません。 はキュー内のメッセージを削除しないため Peek 、 の呼び出しによってロールバックするもの Abortはありません。

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

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

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

こちらもご覧ください

適用対象

Receive(TimeSpan, Cursor, MessageQueueTransactionType)

指定したカーソルを使用して、キューの現在のメッセージを受信します。 利用可能なメッセージがない場合、このメソッドは、メッセージが利用可能になるまで、またはタイムアウトが発生するまで待機します。

public:
 System::Messaging::Message ^ Receive(TimeSpan timeout, System::Messaging::Cursor ^ cursor, System::Messaging::MessageQueueTransactionType transactionType);
public System.Messaging.Message Receive (TimeSpan timeout, System.Messaging.Cursor cursor, System.Messaging.MessageQueueTransactionType transactionType);
member this.Receive : TimeSpan * System.Messaging.Cursor * System.Messaging.MessageQueueTransactionType -> System.Messaging.Message
Public Function Receive (timeout As TimeSpan, cursor As Cursor, transactionType As MessageQueueTransactionType) As Message

パラメーター

timeout
TimeSpan

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

cursor
Cursor

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

transactionType
MessageQueueTransactionType

メッセージと関連付けるトランザクション コンテキストの種類を示す、MessageQueueTransactionType 値の 1 つ。

戻り値

キューのメッセージを参照する Message

例外

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

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

transactionType パラメーターが、MessageQueueTransactionType メンバーの 1 つではありません。

タイムアウトが経過する前に、キューにメッセージが到達しませんでした。

- または -

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

注釈

このオーバーロードを使用して、 パラメーターで定義されたトランザクション コンテキストを使用してキューからメッセージを transactionType 受信し、キューにメッセージがない場合は、指定した期間内にを返します。

メッセージの受信に使用するtransactionTypeスレッドに外部トランザクション コンテキストが既にアタッチされている場合は、 パラメーターに を指定Automaticします。 メッセージを 1 つの内部トランザクションとして受信するかどうかを指定 Single します。 トランザクション コンテキストの外部のトランザクション キューからメッセージを受信するかどうかを指定 None できます。

メソッドを Receive 使用すると、メッセージを同期的に読み取り、キューからメッセージを削除できます。 キュー内 Receive の後続のメッセージを返す後続の呼び出し。

トランザクション キューからメッセージを受信するためにこのメソッドが呼び出された場合、トランザクションが中止されると、受信したメッセージがキューに返されます。 トランザクションがコミットされるまで、メッセージはキューから完全に削除されません。

キューからメッセージを削除せずにキュー内のメッセージを読み取る場合は、 メソッドを使用します Peek 。 への Peek呼び出しによって返されるメッセージに関連付けられたトランザクション コンテキストはありません。 はキュー内のメッセージを削除しないため Peek 、 の呼び出しによってロールバックするもの Abortはありません。

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

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

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

こちらもご覧ください

適用対象

スレッド セーフ

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