MessageQueue.Receive 方法

定義

接收佇列中的第一個訊息,並將其從佇列中移除。

多載

名稱 Description
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 A 指的是佇列中第一個可用的訊息。

例外狀況

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

範例

以下程式碼範例接收佇列中的訊息,並將該訊息的資訊輸出到螢幕。

#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

下表顯示此方法是否可在多種工作群組模式中使用。

工作群組模式 Available
本機電腦 Yes
本地電腦與直接格式名稱 Yes
遠端電腦 No
遠端電腦與直接格式名稱 Yes

另請參閱

適用於

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 A 指的是佇列中第一個可用的訊息。

例外狀況

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

-或-

佇列是非交易性的。

範例

以下程式碼範例連接至本地電腦的交易佇列,並向佇列發送訊息。 接著接收包含訂單的訊息。 如果遇到非交易佇列,會拋出 and 異常並回滾交易。

#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 )
      {
         // 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)
            {
                // 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

下表顯示此方法是否可在多種工作群組模式中使用。

工作群組模式 Available
本機電腦 Yes
本地電腦與直接格式名稱 Yes
遠端電腦 No
遠端電腦與直接格式名稱 Yes

另請參閱

適用於

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 A 指的是佇列中第一個可用的訊息。

例外狀況

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

參數 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 的交易上下文中接收訊息,或等待佇列中有訊息。

請指定AutomatictransactionType參數,是否已經連接到你想用來接收訊息的執行緒。 請指定 Single 你是否想將訊息作為單一內部交易接收。 你可以指定 None 是否想在交易上下文外接收來自交易隊列的訊息。

Receive 方法允許同步讀取訊息,從而將其從佇列中移除。 後續呼叫 會 Receive 回傳隊列中後續的訊息。

若呼叫此方法以接收交易佇列中的訊息,交易中止時,收到的訊息會被回傳至佇列。 在交易提交之前,訊息不會永久從佇列中移除。

若要讀取隊列中的第一則訊息而不將其從佇列中移除,請使用該 Peek 方法。 該 Peek 方法總是回傳佇列中的第一個訊息,因此後續呼叫該方法也會回傳相同的訊息,除非佇列中有更高優先權的訊息。 呼叫回 Peek傳的訊息沒有交易上下文。 由於 Peek 不會移除佇列中的任何訊息,因此無法透過呼叫 Abort來回滾。

在等待訊息到達隊列時,使用呼叫 當 Receive 目前執行緒被封鎖時。 由於方法的過載 Receive 指定了無限的逾時,應用程式可能會無限期等待。 若應用程式處理應繼續且不等待訊息,請考慮使用非同步方法。 BeginReceive

下表顯示此方法是否可在多種工作群組模式中使用。

工作群組模式 Available
本機電腦 Yes
本地電腦與直接格式名稱 Yes
遠端電腦 No
遠端電腦與直接格式名稱 Yes

另請參閱

適用於

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 A表示等待新訊息可檢查的時間。

傳回

Message A 指的是佇列中第一個可用的訊息。

例外狀況

參數指定的 timeout 值不成立,可能 timeoutZero 於或大於 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

下表顯示此方法是否可在多種工作群組模式中使用。

工作群組模式 Available
本機電腦 Yes
本地電腦與直接格式名稱 Yes
遠端電腦 No
遠端電腦與直接格式名稱 Yes

另請參閱

適用於

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 A表示等待新訊息可檢查的時間。

cursor
Cursor

A Cursor 在訊息佇列中維持特定位置。

傳回

Message A 指的是佇列中第一個可用的訊息。

例外狀況

參數指定的 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 A表示等待新訊息可檢查的時間。

傳回

Message A 指的是佇列中第一個可用的訊息。

例外狀況

參數指定的 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)
      {
         // 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)
            {
                // 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

下表顯示此方法是否可在多種工作群組模式中使用。

工作群組模式 Available
本機電腦 Yes
本地電腦與直接格式名稱 Yes
遠端電腦 No
遠端電腦與直接格式名稱 Yes

另請參閱

適用於

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 A表示等待新訊息可檢查的時間。

transactionType
MessageQueueTransactionType

其中一個 MessageQueueTransactionType 值,描述要與訊息關聯的交易上下文類型。

傳回

Message A 指的是佇列中第一個可用的訊息。

例外狀況

參數指定的 timeout 值不成立,可能 timeoutZero 於或大於 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 的交易上下文的訊息,若隊列中無訊息,則在指定時間內返回。

請指定AutomatictransactionType參數,是否已經連接到你想用來接收訊息的執行緒。 請指定 Single 你是否想將訊息作為單一內部交易接收。 你可以指定 None 是否想在交易上下文外接收來自交易隊列的訊息。

Receive 方法允許同步讀取訊息,從而將其從佇列中移除。 後續呼叫 會 Receive 回傳隊列中後續的訊息。

若呼叫此方法以接收交易佇列中的訊息,交易中止時,收到的訊息會被回傳至佇列。 在交易提交之前,訊息不會永久從佇列中移除。

若要讀取隊列中的第一則訊息而不將其從佇列中移除,請使用該 Peek 方法。 該 Peek 方法總是回傳佇列中的第一個訊息,因此後續呼叫該方法也會回傳相同的訊息,除非佇列中有更高優先權的訊息。 呼叫回 Peek傳的訊息沒有交易上下文。 由於 Peek 不會移除佇列中的任何訊息,因此無法透過呼叫 Abort來回滾。

在等待訊息到達隊列時,使用呼叫 當 Receive 目前執行緒被封鎖時。 執行緒會在指定時間內被封鎖,若你指定參數值InfiniteTimeouttimeout則無限期封鎖。 若應用程式處理應繼續且不等待訊息,請考慮使用非同步方法。 BeginReceive

下表顯示此方法是否可在多種工作群組模式中使用。

工作群組模式 Available
本機電腦 Yes
本地電腦與直接格式名稱 Yes
遠端電腦 No
遠端電腦與直接格式名稱 Yes

另請參閱

適用於

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 A表示等待新訊息可檢查的時間。

cursor
Cursor

A Cursor 在訊息佇列中維持特定位置。

傳回

Message A 會參考佇列中的訊息。

例外狀況

參數 cursornull

-或-

參數 transactionnull

timeout 參數指定的值無效。 可能 timeoutZero 於或大於 InfiniteTimeout

訊息未在逾時期結束前送達隊列。

-或-

佇列是非交易性的。

-或-

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

備註

利用此超載方式,利用參數定義 transaction 的內部交易上下文從交易佇列接收訊息,若隊列中無訊息,則在指定時間內返回。

Receive 方法允許同步讀取訊息,從而將其從佇列中移除。 後續呼叫以回 Receive 傳隊列中後續訊息。

由於此方法是在交易佇列中呼叫,若交易中止,收到的訊息會返回佇列。 在交易提交之前,訊息不會永久從佇列中移除。

若要在不移除隊列的情況下讀取訊息,請使用該 Peek 方法。 呼叫回 Peek傳的訊息沒有交易上下文。 由於Peek不會移除佇列中的任何訊息,因此無法透過呼叫回滾。Abort

在等待訊息到達隊列時,使用呼叫 當 Receive 目前執行緒被封鎖時。 執行緒會被封鎖一段時間,或如果你指定參數值InfiniteTimeouttimeout則無限期被封鎖。 若應用程式處理應繼續且不等待訊息,請考慮使用非同步方法。 BeginReceive

下表顯示此方法是否可在多種工作群組模式中使用。

工作群組模式 Available
本機電腦 Yes
本地電腦與直接格式名稱 Yes
遠端電腦 No
遠端電腦與直接格式名稱 Yes

另請參閱

適用於

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 A表示等待新訊息可檢查的時間。

cursor
Cursor

A Cursor 在訊息佇列中維持特定位置。

transactionType
MessageQueueTransactionType

MessageQueueTransactionType其中一個值描述了要與訊息關聯的交易上下文類型。

傳回

Message A 會參考佇列中的訊息。

例外狀況

參數 cursornull

timeout 參數指定的值無效。 可能 timeoutZero 於或大於 InfiniteTimeout

參數 transactionType 並非 MessageQueueTransactionType 成員之一。

訊息未在逾時期結束前送達隊列。

-或-

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

備註

利用此超載從佇列接收由參數定義 transactionType 的交易上下文的訊息,若隊列中無訊息,則在指定時間內返回。

請指定AutomatictransactionType參數,是否已經連接到你想用來接收訊息的執行緒。 請指定 Single 你是否想將訊息作為單一內部交易接收。 你可以指定 None 是否想在交易上下文外接收來自交易隊列的訊息。

Receive 方法允許同步讀取訊息,從而將其從佇列中移除。 後續呼叫以回 Receive 傳隊列中後續訊息。

若呼叫此方法以接收交易佇列中的訊息,交易中止時接收的訊息會回傳至佇列。 在交易提交之前,訊息不會永久從佇列中移除。

若要在不移除隊列的情況下讀取訊息,請使用該 Peek 方法。 呼叫回 Peek傳的訊息沒有交易上下文。 由於Peek不會移除佇列中的任何訊息,因此無法透過呼叫回滾。Abort

在等待訊息到達隊列時,使用呼叫 當 Receive 目前執行緒被封鎖時。 執行緒會被封鎖一段時間,或如果你指定參數值InfiniteTimeouttimeout則無限期被封鎖。 若應用程式處理應繼續且不等待訊息,請考慮使用非同步方法。 BeginReceive

下表顯示此方法是否可在多種工作群組模式中使用。

工作群組模式 Available
本機電腦 Yes
本地電腦與直接格式名稱 Yes
遠端電腦 No
遠端電腦與直接格式名稱 Yes

另請參閱

適用於

執行緒安全性

這個方法並不安全於執行緒。