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

參數

傳回

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 值,描述要與訊息相關聯的異動內容的類型。

傳回

Message,參考佇列中的第一個可用訊息。

例外狀況

存取訊息佇列方法時發生錯誤。

transactionType 參數不是其中一個 MessageQueueTransactionType 成員。

範例

下列程式碼範例示範 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 定義的交易內容從佇列接收訊息,或等到佇列中有訊息為止。

Automatic如果已經附加至您要用來接收訊息之線程的外部交易內容,請指定 transactionType 參數。 指定 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 參數指定的值無效,可能是 timeout 小於 Zero 或大於 InfiniteTimeout

訊息沒有在逾時到期前到達佇列。

-或-

存取訊息佇列方法時發生錯誤

範例

下列程式代碼範例會從佇列接收訊息,並將該訊息的相關信息輸出到畫面。 此範例會在等候訊息抵達佇列時暫停執行最多五秒。

#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 。 如果指定參數的值InfiniteTimeouttimeout,線程將會在指定的時段內遭到封鎖,或無限期地封鎖線程。 如果應用程式處理應該繼續而不等待訊息,請考慮使用異步方法 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 參數指定的值無效,可能是 timeout 小於 Zero 或大於 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,指出等待新訊息可以進行檢查的時間。

傳回

Message,參考佇列中的第一個可用訊息。

例外狀況

timeout 參數指定的值無效,可能是 timeout 小於 Zero 或大於 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 。 如果指定參數的值InfiniteTimeouttimeout,線程將會在指定的時段內遭到封鎖,或無限期地封鎖線程。 如果應用程式處理應該繼續而不等待訊息,請考慮使用異步方法 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 值,描述要與訊息相關聯的異動內容的類型。

傳回

Message,參考佇列中的第一個可用訊息。

例外狀況

timeout 參數指定的值無效,可能是 timeout 小於 Zero 或大於 InfiniteTimeout

transactionType 參數不是其中一個 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);

// 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 定義的交易內容從佇列接收訊息,並在佇列中沒有訊息時,於指定的時段內傳回 。

Automatic如果已經附加至您要用來接收訊息之線程的外部交易內容,請指定 transactionType 參數。 指定 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,保留訊息佇列中的特定位置。

傳回

Message,參考佇列中的訊息。

例外狀況

cursor 參數為 null

-或-

transaction 參數為 null

指定給 timeout 參數的值無效。 可能是 timeout 小於 Zero 或大於 InfiniteTimeout

訊息沒有在逾時到期前到達佇列。

-或-

該佇列是非交易式佇列。

-或-

存取訊息佇列方法時發生錯誤。

備註

使用此多載可從交易佇列接收訊息,使用 參數所 transaction 定義的內部交易內容,並在指定時間內傳回,如果佇列中沒有訊息,則傳回 。

方法 Receive 允許同步讀取訊息,藉此從佇列中移除訊息。 後續呼叫,以 Receive 傳回佇列中後續的訊息。

因為這個方法是在交易佇列上呼叫,所以如果交易已中止,就會將收到的訊息傳回至佇列。 在認可交易之前,不會從佇列永久移除訊息。

若要讀取佇列中的訊息,而不從佇列中移除訊息,請使用 Peek 方法。 呼叫 所 Peek傳回的訊息沒有相關聯的交易內容。 因為 Peek 不會移除佇列中的任何訊息,所以呼叫 不會復原 Abort任何訊息。

當目前線程等候訊息抵達佇列時,可以使用呼叫 Receive 來封鎖目前的線程。 如果指定了 參數的值InfiniteTimeouttimeout,線程會封鎖指定一段時間,或無限期地封鎖線程。 如果應用程式處理應該繼續而不等候訊息,請考慮使用異步方法 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 值,描述要與訊息產生關聯的交易內容類型。

傳回

Message,參考佇列中的訊息。

例外狀況

cursor 參數為 null

指定給 timeout 參數的值無效。 可能是 timeout 小於 Zero 或大於 InfiniteTimeout

transactionType 參數不是其中一個 MessageQueueTransactionType 成員。

訊息沒有在逾時到期前到達佇列。

-或-

存取訊息佇列方法時發生錯誤。

備註

使用此多載,使用 參數所 transactionType 定義的交易內容從佇列接收訊息,並在佇列中沒有訊息時,於指定的時段內傳回 。

Automatic如果已經附加至您要用來接收訊息之線程的外部交易內容,請指定 transactionType 參數。 指定 Single 是否要以單一內部交易的形式接收訊息。 您可以指定 None 是否要從交易內容外部的交易佇列接收訊息。

方法 Receive 允許同步讀取訊息,藉此從佇列中移除訊息。 後續呼叫,以 Receive 傳回佇列中後續的訊息。

如果呼叫這個方法以接收來自交易佇列的訊息,如果交易已中止,則會將收到的訊息傳回至佇列。 在認可交易之前,不會從佇列永久移除訊息。

若要讀取佇列中的訊息,而不從佇列中移除訊息,請使用 Peek 方法。 呼叫 所 Peek傳回的訊息沒有相關聯的交易內容。 因為 Peek 不會移除佇列中的任何訊息,所以呼叫 不會復原 Abort任何訊息。

當目前線程等候訊息抵達佇列時,可以使用呼叫 Receive 來封鎖目前的線程。 如果指定了 參數的值InfiniteTimeouttimeout,線程會封鎖指定一段時間,或無限期地封鎖線程。 如果應用程式處理應該繼續而不等候訊息,請考慮使用異步方法 BeginReceive

下表顯示這個方法是否可在各種工作組模式中使用。

工作組模式 可用
本機電腦
本機計算機和直接格式名稱
遠端電腦
遠端電腦和直接格式名稱

另請參閱

適用於

執行緒安全性

方法不是安全線程。