MessageQueue.Receive 方法

定义

接收队列中的第一条消息,将其从队列中删除。

重载

名称 说明
Receive()

接收队列中第一条消息,该消息由 <a0/&a0> 引用。 此调用是同步的,并阻止当前执行线程,直到消息可用。

Receive(MessageQueueTransaction)

接收事务队列中第一条消息,该消息由 <a0/&a0> 引用。 此调用是同步的,并阻止当前执行线程,直到消息可用。

Receive(MessageQueueTransactionType)

接收队列中第一条消息,该消息由 <a0/&a0> 引用。 此调用是同步的,并阻止当前执行线程,直到消息可用。

Receive(TimeSpan)

接收队列中第一条可用消息, MessageQueue 并等待消息在队列中可用,或者超时过期。

Receive(TimeSpan, Cursor)

使用指定的游标接收队列中的当前消息。 如果没有消息可用,此方法将等到消息可用或超时过期。

Receive(TimeSpan, MessageQueueTransaction)

接收事务队列 MessageQueue 中第一条可用消息,并等待消息在队列中可用或超时过期。

Receive(TimeSpan, MessageQueueTransactionType)

接收队列中第一条消息,该消息由 <a0/&a0> 引用。 此调用是同步的,等待消息在队列中可用或超时过期。

Receive(TimeSpan, Cursor, MessageQueueTransaction)

使用指定的游标接收队列中的当前消息。 如果没有消息可用,此方法将等到消息可用或超时过期。

Receive(TimeSpan, Cursor, MessageQueueTransactionType)

使用指定的游标接收队列中的当前消息。 如果没有消息可用,此方法将等到消息可用或超时过期。

Receive()

接收队列中第一条消息,该消息由 <a0/&a0> 引用。 此调用是同步的,并阻止当前执行线程,直到消息可用。

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)

接收事务队列中第一条消息,该消息由 <a0/&a0> 引用。 此调用是同步的,并阻止当前执行线程,直到消息可用。

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

下表显示了此方法在各种工作组模式下是否可用。

工作组模式 可用的
本地计算机
本地计算机和直接格式名称
远程计算机
远程计算机和直接格式名称

另请参阅

适用于

Receive(MessageQueueTransactionType)

接收队列中第一条消息,该消息由 <a0/&a0> 引用。 此调用是同步的,并阻止当前执行线程,直到消息可用。

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

超时过期之前,消息未到达队列。

-或-

访问消息队列方法时出错

示例

下面的代码示例从队列接收消息,并将有关该消息的信息输出到屏幕。 该示例在等待消息到达队列时暂停执行最多 5 秒。

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

using namespace System;
using namespace System::Messaging;

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


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

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

      // Set the formatter to indicate body contains an Order.
      array<Type^>^p = gcnew array<Type^>(1);
      p[ 0 ] = Order::typeid;
      myQueue->Formatter = gcnew XmlMessageFormatter( p );
      try
      {
         // Receive and format the message. 
         // Wait 5 seconds for a message to arrive.
         Message^ myMessage = myQueue->Receive( TimeSpan(0,0,5) );
         Order^ myOrder = static_cast<Order^>(myMessage->Body);

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

         // Handle other sources of a MessageQueueException.
      }
      // Handle invalid serialization format.
      catch ( InvalidOperationException^ e ) 
      {
         Console::WriteLine( e->Message );
      }

      // Catch other exceptions as necessary.
      return;
   }
};

//*************************************************
// Provides an entry point into the application.
//         
// This example receives a message from a queue.
//*************************************************
int main()
{
   // Create a new instance of the class.
   MyNewQueue^ myNewQueue = gcnew MyNewQueue;

   // Receive a message from a queue.
   myNewQueue->ReceiveMessage();
   return 0;
}
using System;
using System.Messaging;

namespace MyProject
{
    // This class represents an object the following example
    // receives from a queue.

    public class Order
    {
        public int orderId;
        public DateTime orderTime;
    };	

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

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

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

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

            return;
        }

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

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

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

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

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

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

            return;
        }
    }
}
Imports System.Messaging

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


   
Public Class MyNewQueue


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

        Public Shared Sub Main()

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

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

            Return

        End Sub


        '
        ' Receives a message containing an Order.
        '

        Public Sub ReceiveMessage()

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

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

            Try

                ' Receive and format the message. 
                ' Wait 5 seconds for a message to arrive.
                Dim myMessage As Message = myQueue.Receive(New _
                    TimeSpan(0, 0, 5))
                Dim myOrder As Order = CType(myMessage.Body, Order)

                ' Display message information.
                Console.WriteLine(("Order ID: " + _
                    myOrder.orderId.ToString()))
                Console.WriteLine(("Sent: " + _
                    myOrder.orderTime.ToString()))

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

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

                End If

                ' Handle other sources of a MessageQueueException.

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

                ' Catch other exceptions as necessary.

            End Try

            Return

        End Sub

End Class

注解

使用此重载接收消息,并在指定时间段内返回(如果队列中没有消息)。

该方法 Receive 允许同步读取消息,并将其从队列中删除。 后续调用 Receive 将返回队列中后面的消息,或新的高优先级消息。

若要读取队列中的第一条消息而不将其从队列中删除,请使用 Peek 该方法。 该方法 Peek 始终返回队列中的第一条消息,因此对该方法的后续调用将返回相同的消息,除非优先级较高的消息到达队列。

当当前线程等待消息到达队列时,可以使用调用 Receive 来阻止当前线程。 如果为参数指定了值 InfiniteTimeout ,线程将在给定的时间段内被阻止,或无限期地阻止该 timeout 线程。 如果应用程序处理应继续而不等待消息,请考虑使用异步方法 BeginReceive

下表显示了此方法在各种工作组模式下是否可用。

工作组模式 可用的
本地计算机
本地计算机和直接格式名称
远程计算机
远程计算机和直接格式名称

另请参阅

适用于

Receive(TimeSpan, Cursor)

使用指定的游标接收队列中的当前消息。 如果没有消息可用,此方法将等到消息可用或超时过期。

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

参数

timeout
TimeSpan

指示 TimeSpan 等待新消息可供检查的时间。

cursor
Cursor

维护消息队列中的特定位置的 A 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)
      {
         // 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 来阻止当前线程。 如果为参数指定了值 InfiniteTimeout ,线程将在给定的时间段内被阻止,或无限期地阻止该 timeout 线程。 如果应用程序处理应继续而不等待消息,请考虑使用异步方法 BeginReceive

下表显示了此方法在各种工作组模式下是否可用。

工作组模式 可用的
本地计算机
本地计算机和直接格式名称
远程计算机
远程计算机和直接格式名称

另请参阅

适用于

Receive(TimeSpan, MessageQueueTransactionType)

接收队列中第一条消息,该消息由 <a0/&a0> 引用。 此调用是同步的,等待消息在队列中可用或超时过期。

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

维护消息队列中的特定位置的 A Cursor

返回

引用队列中的消息的一 Message 个。

例外

参数 cursornull.

-或-

参数 transactionnull.

timeout 参数指定的值无效。 timeout可能小于Zero或大于 InfiniteTimeout

超时过期之前,消息未到达队列。

-或-

队列是非事务性的。

-或-

访问消息队列方法时出错。

注解

使用此重载通过参数定义的 transaction 内部事务上下文从事务队列接收消息,并在指定时间段内返回(如果队列中没有消息)。

该方法 Receive 允许同步读取消息,从而将其从队列中删除。 后续调用以 Receive 返回队列中的后续消息。

由于此方法在事务队列上调用,因此如果中止事务,则接收的消息将返回到队列。 在提交事务之前,消息不会永久从队列中删除。

若要在队列中读取消息而不将其从队列中删除,请使用 Peek 该方法。 没有与调用 Peek返回的消息关联的事务上下文。 由于 Peek 不会删除队列中的任何消息,因此 Abort调用不会回滚任何消息。

当当前线程等待消息到达队列时,可以使用调用 Receive 来阻止当前线程。 线程在给定时间段内被阻止,或者,如果为参数指定了值 InfiniteTimeout ,则无限期地阻止该 timeout 线程。 如果应用程序处理应继续而不等待消息,请考虑使用异步方法 BeginReceive

下表显示了此方法在各种工作组模式下是否可用。

工作组模式 可用的
本地计算机
本地计算机和直接格式名称
远程计算机
远程计算机和直接格式名称

另请参阅

适用于

Receive(TimeSpan, Cursor, MessageQueueTransactionType)

使用指定的游标接收队列中的当前消息。 如果没有消息可用,此方法将等到消息可用或超时过期。

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

参数

timeout
TimeSpan

指示 TimeSpan 等待新消息可供检查的时间。

cursor
Cursor

维护消息队列中的特定位置的 A Cursor

transactionType
MessageQueueTransactionType

描述 MessageQueueTransactionType 要与消息关联的事务上下文类型的值之一。

返回

引用队列中的消息的一 Message 个。

例外

参数 cursornull.

timeout 参数指定的值无效。 timeout可能小于Zero或大于 InfiniteTimeout

transactionType 参数不是其中一个 MessageQueueTransactionType 成员。

超时过期之前,消息未到达队列。

-或-

访问消息队列方法时出错。

注解

使用此重载可以使用参数定义的 transactionType 事务上下文从队列接收消息,并在指定时间段内返回(如果队列中没有消息)。

Automatic如果已将外部事务上下文附加到要用于接收消息的线程,请指定transactionType该参数。 指定 Single 是否要以单个内部事务的形式接收消息。 可以指定 None 是否要从事务上下文之外的事务队列接收消息。

该方法 Receive 允许同步读取消息,从而将其从队列中删除。 后续调用以 Receive 返回队列中的后续消息。

如果调用此方法以从事务队列接收消息,则如果中止事务,则接收的消息将返回到队列。 在提交事务之前,消息不会永久从队列中删除。

若要在队列中读取消息而不将其从队列中删除,请使用 Peek 该方法。 没有与调用 Peek返回的消息关联的事务上下文。 由于 Peek 不会删除队列中的任何消息,因此 Abort调用不会回滚任何消息。

当当前线程等待消息到达队列时,可以使用调用 Receive 来阻止当前线程。 线程在给定时间段内被阻止,或者,如果为参数指定了值 InfiniteTimeout ,则无限期地阻止该 timeout 线程。 如果应用程序处理应继续而不等待消息,请考虑使用异步方法 BeginReceive

下表显示了此方法在各种工作组模式下是否可用。

工作组模式 可用的
本地计算机
本地计算机和直接格式名称
远程计算机
远程计算机和直接格式名称

另请参阅

适用于

线程安全性

该方法不是线程安全的。