MessageQueue.Receive Metoda

Definice

Přijme první zprávu ve frontě a odebere ji z fronty.

Přetížení

Receive()

Obdrží první zprávu dostupnou ve frontě, na kterou MessageQueueodkazuje . Toto volání je synchronní a blokuje aktuální vlákno spuštění, dokud není k dispozici zpráva.

Receive(MessageQueueTransaction)

Obdrží první zprávu dostupnou v transakční frontě, na kterou MessageQueueodkazuje . Toto volání je synchronní a blokuje aktuální vlákno spuštění, dokud není k dispozici zpráva.

Receive(MessageQueueTransactionType)

Obdrží první zprávu dostupnou ve frontě, na kterou MessageQueueodkazuje . Toto volání je synchronní a blokuje aktuální vlákno spuštění, dokud není k dispozici zpráva.

Receive(TimeSpan)

Přijme první zprávu dostupnou ve frontě, na kterou MessageQueue odkazuje, a počká, dokud nebude zpráva ve frontě k dispozici nebo časový limit vyprší.

Receive(TimeSpan, Cursor)

Přijme aktuální zprávu ve frontě pomocí zadaného kurzoru. Pokud není k dispozici žádná zpráva, tato metoda počká, dokud nebude zpráva k dispozici nebo časový limit vyprší.

Receive(TimeSpan, MessageQueueTransaction)

Přijme první zprávu dostupnou v transakční frontě, na kterou MessageQueue odkazuje, a počká, dokud nebude zpráva ve frontě k dispozici, nebo dokud nevyprší časový limit.

Receive(TimeSpan, MessageQueueTransactionType)

Obdrží první zprávu dostupnou ve frontě, na kterou MessageQueueodkazuje . Toto volání je synchronní a čeká na dostupnost zprávy ve frontě nebo vypršení časového limitu.

Receive(TimeSpan, Cursor, MessageQueueTransaction)

Přijme aktuální zprávu ve frontě pomocí zadaného kurzoru. Pokud není k dispozici žádná zpráva, tato metoda počká, dokud nebude zpráva k dispozici nebo časový limit vyprší.

Receive(TimeSpan, Cursor, MessageQueueTransactionType)

Přijme aktuální zprávu ve frontě pomocí zadaného kurzoru. Pokud není k dispozici žádná zpráva, tato metoda počká, dokud nebude zpráva k dispozici nebo časový limit vyprší.

Receive()

Obdrží první zprávu dostupnou ve frontě, na kterou MessageQueueodkazuje . Toto volání je synchronní a blokuje aktuální vlákno spuštění, dokud není k dispozici zpráva.

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

Návraty

A Message , který odkazuje na první zprávu dostupnou ve frontě.

Výjimky

Při přístupu k metodě služby Řízení front zpráv došlo k chybě.

Příklady

Následující příklad kódu přijme zprávu z fronty a vypíše informace o této zprávě na obrazovku.

#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

Poznámky

Toto přetížení použijte k příjmu zpráv z fronty nebo k čekání na zprávy ve frontě.

Metoda Receive umožňuje synchronní čtení zprávy a tím ji odebere z fronty. Další volání Receive metody vrátí následující zprávy ve frontě nebo nové zprávy s vyšší prioritou.

Pokud chcete přečíst první zprávu ve frontě, aniž byste ji z fronty odebrali, použijte metodu Peek . Metoda Peek vždy vrátí první zprávu ve frontě, takže další volání metody vrátí stejnou zprávu, pokud do fronty nedorazí zpráva s vyšší prioritou.

Volání použijte, Receive pokud je přijatelné, aby aktuální vlákno bylo zablokováno, zatímco čeká na doručení zprávy do fronty. Vzhledem k tomu, že toto přetížení Receive metody určuje nekonečný časový limit, aplikace může čekat neomezeně dlouho. Pokud by zpracování aplikace mělo pokračovat bez čekání na zprávu, zvažte použití asynchronní metody BeginReceive.

Následující tabulka ukazuje, zda je tato metoda k dispozici v různých režimech pracovní skupiny.

Režim pracovní skupiny K dispozici.
Místní počítač Yes
Název místního počítače a přímého formátu Yes
Vzdálený počítač No
Název vzdáleného počítače a přímého formátu Yes

Viz také

Platí pro

Receive(MessageQueueTransaction)

Obdrží první zprávu dostupnou v transakční frontě, na kterou MessageQueueodkazuje . Toto volání je synchronní a blokuje aktuální vlákno spuštění, dokud není k dispozici zpráva.

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

Parametry

Návraty

A Message , který odkazuje na první zprávu dostupnou ve frontě.

Výjimky

Při přístupu k metodě služby Řízení front zpráv došlo k chybě.

-nebo-

Fronta není transakční.

Příklady

Následující příklad kódu se připojí k transakční frontě v místním počítači a odešle zprávu do fronty. Potom obdrží zprávu, která obsahuje objednávku. Pokud narazí na ne transactionální frontu, vyvolá výjimku a vrátí zpět transakci.

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

using namespace System;
using namespace System::Messaging;

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

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

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

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

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

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

      return;
   }


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

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

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

         // Receive the message. 
         Message^ myMessage = myQueue->Receive( myTransaction );
         String^ myOrder = static_cast<String^>(myMessage->Body);

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

         // Commit the transaction.
         myTransaction->Commit();
      }
      catch ( MessageQueueException^ e ) 
      {
         // Handle nontransactional queues.
         if ( e->MessageQueueErrorCode == MessageQueueErrorCode::TransactionUsage )
         {
            Console::WriteLine( "Queue is not transactional." );
         }

         // Else catch other sources of a MessageQueueException.
         // Roll back the transaction.
         myTransaction->Abort();
      }

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

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

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

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

namespace MyProject
{

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

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

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

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

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

            return;
        }

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

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

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

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

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

            return;
        }

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

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

            try
            {
                // Begin the transaction.
                myTransaction.Begin();
                
                // Receive the message.
                Message myMessage =	myQueue.Receive(myTransaction);
                String myOrder = (String)myMessage.Body;

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

                // Commit the transaction.
                myTransaction.Commit();
            }
            
            catch (MessageQueueException e)
            {
                // Handle nontransactional queues.
                if (e.MessageQueueErrorCode ==
                    MessageQueueErrorCode.TransactionUsage)
                {
                    Console.WriteLine("Queue is not transactional.");
                }
                
                // Else catch other sources of a MessageQueueException.

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

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

            return;
        }
    }
}
Imports System.Messaging

Public Class MyNewQueue


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

        Public Shared Sub Main()

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

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

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

            Return

        End Sub


        '
        ' Sends a message to a queue.
        '

        Public Sub SendMessageTransactional()

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

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

                ' Create a transaction.
                Dim myTransaction As New MessageQueueTransaction

                ' Begin the transaction.
                myTransaction.Begin()

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

                ' Commit the transaction.
                myTransaction.Commit()

            End If

            Return

        End Sub


        '
        ' Receives a message containing an Order.
        '

        Public Sub ReceiveMessageTransactional()

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

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

            ' Create a transaction.
            Dim myTransaction As New MessageQueueTransaction

            Try

                ' Begin the transaction.
                myTransaction.Begin()

                ' Receive the message. 
                Dim myMessage As Message = _
                    myQueue.Receive(myTransaction)
                Dim myOrder As [String] = CType(myMessage.Body, _
                    [String])

                ' Display message information.
                Console.WriteLine(myOrder)

                ' Commit the transaction.
                myTransaction.Commit()


            Catch e As MessageQueueException

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

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

                End If

                ' Else catch other sources of a MessageQueueException.


                ' Roll back the transaction.
                myTransaction.Abort()


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

            End Try

            Return

        End Sub

End Class

Poznámky

Toto přetížení použijte k přijetí zprávy z transakční fronty pomocí interního kontextu transakce definovaného parametrem transaction nebo čekání na zprávy ve frontě.

Metoda Receive umožňuje synchronní čtení zprávy a tím ji odebere z fronty. Další volání metody Receive vrátí zprávy, které následují ve frontě.

Vzhledem k tomu, že tato metoda je volána v transakční frontě, zpráva, která je přijata, bude vrácena do fronty, pokud transakce je přerušena. Zpráva není trvale odebrána z fronty, dokud transakce není potvrzena.

Pokud chcete přečíst první zprávu ve frontě, aniž byste ji z fronty odebrali, použijte metodu Peek . Metoda Peek vždy vrátí první zprávu ve frontě, takže další volání metody vrátí stejnou zprávu, pokud do fronty nedorazí zpráva s vyšší prioritou. Ke zprávě vrácené voláním Peeknení přidružen žádný kontext transakce. Vzhledem k tomu Peek , že neodebere žádné zprávy ve frontě, nebylo by nic, co by bylo možné vrátit voláním metody Abort.

Volání použijte, Receive pokud je přijatelné, aby aktuální vlákno bylo zablokováno, zatímco čeká na doručení zprávy do fronty. Vzhledem k tomu, že toto přetížení Receive metody určuje nekonečný časový limit, aplikace může čekat neomezeně dlouho. Pokud by zpracování aplikace mělo pokračovat bez čekání na zprávu, zvažte použití asynchronní metody BeginReceive.

Následující tabulka ukazuje, zda je tato metoda k dispozici v různých režimech pracovní skupiny.

Režim pracovní skupiny K dispozici.
Místní počítač Yes
Název místního počítače a přímého formátu Yes
Vzdálený počítač No
Název vzdáleného počítače a přímého formátu Yes

Viz také

Platí pro

Receive(MessageQueueTransactionType)

Obdrží první zprávu dostupnou ve frontě, na kterou MessageQueueodkazuje . Toto volání je synchronní a blokuje aktuální vlákno spuštění, dokud není k dispozici zpráva.

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

Parametry

transactionType
MessageQueueTransactionType

Jedna z MessageQueueTransactionType hodnot popisující typ kontextu transakce, který se má přidružit ke zprávě.

Návraty

A Message , který odkazuje na první zprávu dostupnou ve frontě.

Výjimky

Při přístupu k metodě služby Řízení front zpráv došlo k chybě.

Parametr transactionType není jedním ze MessageQueueTransactionType členů.

Příklady

Následující příklad kódu ukazuje použití .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);

Poznámky

Toto přetížení použijte k přijetí zprávy z fronty pomocí kontextu transakce definovaného parametrem transactionType nebo čekání na zprávy ve frontě.

Zadejte Automatic jako transactionType parametr, pokud již existuje kontext externí transakce připojený k vláknu, které chcete použít k přijetí zprávy. Určete Single , zda chcete zprávu přijmout jako jednu interní transakci. Můžete určit None , zda chcete přijímat zprávu z transakční fronty mimo kontext transakce.

Metoda Receive umožňuje synchronní čtení zprávy a tím ji odebere z fronty. Další volání metody Receive vrátí zprávy, které následují ve frontě.

Pokud je tato metoda volána k přijetí zprávy z transakční fronty, zpráva, která je přijata, bude vrácena do fronty, pokud transakce je přerušena. Zpráva není trvale odebrána z fronty, dokud transakce není potvrzena.

Pokud chcete přečíst první zprávu ve frontě, aniž byste ji z fronty odebrali, použijte metodu Peek . Metoda Peek vždy vrátí první zprávu ve frontě, takže další volání metody vrátí stejnou zprávu, pokud do fronty nedorazí zpráva s vyšší prioritou. Ke zprávě vrácené voláním Peeknení přidružen žádný kontext transakce. Vzhledem k tomu Peek , že neodebere žádné zprávy ve frontě, nebylo by nic, co by bylo možné vrátit voláním metody Abort.

Volání použijte, Receive pokud je přijatelné, aby aktuální vlákno bylo zablokováno, zatímco čeká na doručení zprávy do fronty. Vzhledem k tomu, že toto přetížení Receive metody určuje nekonečný časový limit, aplikace může čekat neomezeně dlouho. Pokud by zpracování aplikace mělo pokračovat bez čekání na zprávu, zvažte použití asynchronní metody BeginReceive.

Následující tabulka ukazuje, zda je tato metoda k dispozici v různých režimech pracovní skupiny.

Režim pracovní skupiny K dispozici.
Místní počítač Yes
Název místního počítače a přímého formátu Yes
Vzdálený počítač No
Název vzdáleného počítače a přímého formátu Yes

Viz také

Platí pro

Receive(TimeSpan)

Přijme první zprávu dostupnou ve frontě, na kterou MessageQueue odkazuje, a počká, dokud nebude zpráva ve frontě k dispozici nebo časový limit vyprší.

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

Parametry

timeout
TimeSpan

Hodnota TimeSpan označující dobu čekání, než bude k dispozici nová zpráva pro kontrolu.

Návraty

A Message , který odkazuje na první zprávu dostupnou ve frontě.

Výjimky

Hodnota zadaná pro timeout parametr není platná, pravděpodobně timeout je menší než Zero nebo větší než InfiniteTimeout.

Zpráva do fronty nepřišla před vypršením časového limitu.

-nebo-

Při přístupu k metodě služby Řízení front zpráv došlo k chybě.

Příklady

Následující příklad kódu přijme zprávu z fronty a vypíše informace o této zprávě na obrazovku. Příklad pozastaví provádění až na pět sekund při čekání na doručení zprávy do fronty.

#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

Poznámky

Toto přetížení použijte k přijetí zprávy a vrácení v zadaném časovém období, pokud ve frontě nejsou žádné zprávy.

Metoda Receive umožňuje synchronní čtení zprávy a odebere ji z fronty. Další volání Receive metody vrátí následující zprávy ve frontě nebo nové zprávy s vyšší prioritou.

Pokud chcete přečíst první zprávu ve frontě, aniž byste ji z fronty odebrali, použijte metodu Peek . Metoda Peek vždy vrátí první zprávu ve frontě, takže další volání metody vrátí stejnou zprávu, pokud do fronty nedorazí zpráva s vyšší prioritou.

Volání použijte, Receive pokud je přijatelné, aby aktuální vlákno bylo zablokováno, zatímco čeká na doručení zprávy do fronty. Vlákno bude blokováno po dané časové období nebo na neomezenou dobu, pokud jste zadali hodnotu InfiniteTimeout parametru timeout . Pokud by zpracování aplikace mělo pokračovat bez čekání na zprávu, zvažte použití asynchronní metody BeginReceive.

Následující tabulka ukazuje, zda je tato metoda k dispozici v různých režimech pracovní skupiny.

Režim pracovní skupiny K dispozici.
Místní počítač Yes
Název místního počítače a přímého formátu Yes
Vzdálený počítač No
Název vzdáleného počítače a přímého formátu Yes

Viz také

Platí pro

Receive(TimeSpan, Cursor)

Přijme aktuální zprávu ve frontě pomocí zadaného kurzoru. Pokud není k dispozici žádná zpráva, tato metoda počká, dokud nebude zpráva k dispozici nebo časový limit vyprší.

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

Parametry

timeout
TimeSpan

Hodnota TimeSpan označující dobu čekání, než bude k dispozici nová zpráva pro kontrolu.

cursor
Cursor

A Cursor , který udržuje určitou pozici ve frontě zpráv.

Návraty

A Message , který odkazuje na první zprávu dostupnou ve frontě.

Výjimky

Hodnota zadaná pro timeout parametr není platná, pravděpodobně timeout je menší než Zero nebo větší než InfiniteTimeout.

Zpráva do fronty nepřišla před vypršením časového limitu.

-nebo-

Při přístupu k metodě služby Řízení front zpráv došlo k chybě.

Toto přetížení použijte k přijetí zprávy a vrácení v zadaném časovém období, pokud ve frontě nejsou žádné zprávy.

Platí pro

Receive(TimeSpan, MessageQueueTransaction)

Přijme první zprávu dostupnou v transakční frontě, na kterou MessageQueue odkazuje, a počká, dokud nebude zpráva ve frontě k dispozici, nebo dokud nevyprší časový limit.

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

Parametry

timeout
TimeSpan

Hodnota TimeSpan označující dobu čekání, než bude k dispozici nová zpráva pro kontrolu.

Návraty

A Message , který odkazuje na první zprávu dostupnou ve frontě.

Výjimky

Hodnota zadaná pro timeout parametr není platná, pravděpodobně timeout je menší než Zero nebo větší než InfiniteTimeout.

Zpráva do fronty nepřišla před vypršením časového limitu.

-nebo-

Fronta není transakční.

-nebo-

Při přístupu k metodě služby Řízení front zpráv došlo k chybě.

Příklady

Následující příklad kódu ukazuje použití této metody.

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

using namespace System;
using namespace System::Messaging;

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

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

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

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

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

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

      return;
   }

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

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

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

         // Receive the message. 
         // Wait five seconds for a message to arrive. 
         Message^ myMessage = myQueue->Receive( TimeSpan(0,0,5), myTransaction );
         String^ myOrder = static_cast<String^>(myMessage->Body);

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

         // Commit the transaction.
         myTransaction->Commit();
      }
      catch ( MessageQueueException^ e ) 
      {
         // Handle nontransactional queues.
         if ( e->MessageQueueErrorCode == MessageQueueErrorCode::TransactionUsage )
         {
            Console::WriteLine( "Queue is not transactional." );
         }
         // Handle no message arriving in the queue.
         else

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

         // Else catch other sources of MessageQueueException.
         // Roll back the transaction.
         myTransaction->Abort();
      }

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

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

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

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

namespace MyProject
{

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

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

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

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

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

            return;
        }

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

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

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

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

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

            return;
        }

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

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

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

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

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

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

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

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

            return;
        }
    }
}
Imports System.Messaging

Namespace MyProj


   
    Public Class MyNewQueue


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

        Public Shared Sub Main()

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

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

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

            Return

        End Sub


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

        Public Sub SendMessageTransactional()

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

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

                ' Create a transaction.
                Dim myTransaction As New MessageQueueTransaction

                ' Begin the transaction.
                myTransaction.Begin()

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

                ' Commit the transaction.
                myTransaction.Commit()

            End If

            Return

        End Sub


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

        Public Sub ReceiveMessageTransactional()

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

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

            ' Create a transaction.
            Dim myTransaction As New MessageQueueTransaction

            Try

                ' Begin the transaction.
                myTransaction.Begin()

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

                ' Display message information.
                Console.WriteLine(myOrder)

                ' Commit the transaction.
                myTransaction.Commit()


            Catch e As MessageQueueException

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

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

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

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

                    End If
                End If

                ' Else catch other sources of a MessageQueueException.

                ' Roll back the transaction.
                myTransaction.Abort()


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

            End Try

            Return

        End Sub

    End Class
End Namespace 'MyProj

Poznámky

Toto přetížení použijte k přijetí zprávy z transakční fronty pomocí kontextu interní transakce definovaného transaction parametrem a vrácení během zadaného časového období, pokud ve frontě nejsou žádné zprávy.

Metoda Receive umožňuje synchronní čtení zprávy a tím ji odebere z fronty. Další volání metody Receive vrátí zprávy, které následují ve frontě.

Vzhledem k tomu, že tato metoda je volána v transakční frontě, zpráva, která je přijata, bude vrácena do fronty, pokud transakce je přerušena. Zpráva není trvale odebrána z fronty, dokud transakce není potvrzena.

Pokud chcete přečíst první zprávu ve frontě, aniž byste ji z fronty odebrali, použijte metodu Peek . Metoda Peek vždy vrátí první zprávu ve frontě, takže další volání metody vrátí stejnou zprávu, pokud do fronty nedorazí zpráva s vyšší prioritou. Ke zprávě vrácené voláním Peeknení přidružen žádný kontext transakce. Vzhledem k tomu Peek , že neodebere žádné zprávy ve frontě, nebylo by nic, co by bylo možné vrátit voláním metody Abort.

Volání použijte, Receive pokud je přijatelné, aby aktuální vlákno bylo zablokováno, zatímco čeká na doručení zprávy do fronty. Vlákno bude blokováno po dané časové období nebo na neomezenou dobu, pokud jste zadali hodnotu InfiniteTimeout parametru timeout . Pokud by zpracování aplikace mělo pokračovat bez čekání na zprávu, zvažte použití asynchronní metody BeginReceive.

Následující tabulka ukazuje, zda je tato metoda k dispozici v různých režimech pracovní skupiny.

Režim pracovní skupiny K dispozici.
Místní počítač Yes
Název místního počítače a přímého formátu Yes
Vzdálený počítač No
Název vzdáleného počítače a přímého formátu Yes

Viz také

Platí pro

Receive(TimeSpan, MessageQueueTransactionType)

Obdrží první zprávu dostupnou ve frontě, na kterou MessageQueueodkazuje . Toto volání je synchronní a čeká na dostupnost zprávy ve frontě nebo vypršení časového limitu.

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

Parametry

timeout
TimeSpan

Hodnota TimeSpan označující dobu čekání na zpřístupnění nové zprávy pro kontrolu.

transactionType
MessageQueueTransactionType

Jedna z MessageQueueTransactionType hodnot popisující typ kontextu transakce, který se má přidružit ke zprávě.

Návraty

A Message , který odkazuje na první zprávu dostupnou ve frontě.

Výjimky

Hodnota zadaná pro timeout parametr není platná, pravděpodobně timeout je menší než Zero nebo větší než InfiniteTimeout.

Parametr transactionType není jedním ze MessageQueueTransactionType členů.

Zpráva nepřišla do fronty před vypršením časového limitu.

-nebo-

Při přístupu k metodě služby Řízení front zpráv došlo k chybě.

Příklady

Následující příklad kódu ukazuje použití této metody.


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

Poznámky

Toto přetížení použijte k přijetí zprávy z fronty pomocí kontextu transakce definovaného transactionType parametrem a vrácení v zadaném časovém období, pokud ve frontě nejsou žádné zprávy.

Zadejte Automatic pro transactionType parametr, pokud již existuje externí transakční kontext připojený k vláknu, které chcete použít k přijetí zprávy. Určete Single , jestli chcete zprávu přijmout jako jednu interní transakci. Můžete určit None , jestli chcete přijímat zprávu z transakční fronty mimo kontext transakce.

Metoda Receive umožňuje synchronní čtení zprávy, čímž ji odebere z fronty. Následná volání vrátí Receive zprávy, které následují ve frontě.

Pokud je tato metoda volána pro příjem zprávy z transakční fronty, zpráva, která je přijata, bude vrácena do fronty, pokud je transakce přerušena. Zpráva se trvale neodebere z fronty, dokud transakce není potvrzena.

Pokud chcete přečíst první zprávu ve frontě, aniž byste ji z fronty odebrali, použijte metodu Peek . Metoda Peek vždy vrátí první zprávu ve frontě, takže následná volání metody vrátí stejnou zprávu, pokud do fronty nedorazí zpráva s vyšší prioritou. Ke zprávě vrácené voláním Peeknení přidružen žádný kontext transakce. Vzhledem k tomu Peek , že neodebere žádné zprávy ve frontě, nebylo by nic, co by bylo možné vrátit voláním na Abort.

Volání použijte, Receive pokud je přijatelné, aby aktuální vlákno bylo blokováno, zatímco čeká na doručení zprávy do fronty. Vlákno bude zablokované po dané časové období nebo na dobu neurčitou, pokud jste zadali hodnotu InfiniteTimeout parametru timeout . Pokud by zpracování aplikace mělo pokračovat bez čekání na zprávu, zvažte použití asynchronní metody BeginReceive.

Následující tabulka ukazuje, zda je tato metoda k dispozici v různých režimech pracovní skupiny.

Režim pracovní skupiny K dispozici.
Místní počítač Yes
Název místního počítače a přímého formátu Yes
Vzdálený počítač No
Název vzdáleného počítače a přímého formátu Yes

Viz také

Platí pro

Receive(TimeSpan, Cursor, MessageQueueTransaction)

Přijme aktuální zprávu ve frontě pomocí zadaného kurzoru. Pokud není k dispozici žádná zpráva, tato metoda počká, dokud nebude k dispozici zpráva, nebo vyprší časový limit.

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

Parametry

timeout
TimeSpan

Hodnota TimeSpan označující dobu čekání na zpřístupnění nové zprávy pro kontrolu.

cursor
Cursor

A Cursor , který udržuje určitou pozici ve frontě zpráv.

Návraty

A Message , který odkazuje na zprávu ve frontě.

Výjimky

Parametr cursor je null.

-nebo-

Parametr transaction je null.

Hodnota zadaná pro timeout parametr není platná. Možná timeout je menší než Zero nebo větší než InfiniteTimeout.

Zpráva nepřišla do fronty před vypršením časového limitu.

-nebo-

Fronta není transakční.

-nebo-

Při přístupu k metodě služby Řízení front zpráv došlo k chybě.

Poznámky

Toto přetížení použijte k přijetí zprávy z transakční fronty pomocí kontextu interní transakce definovaného transaction parametrem a vrácení během zadaného časového období, pokud ve frontě nejsou žádné zprávy.

Metoda Receive umožňuje synchronní čtení zprávy, čímž ji odebere z fronty. Následná volání pro Receive vrácení zpráv, které následují ve frontě.

Vzhledem k tomu, že tato metoda je volána v transakční frontě, je přijatá zpráva vrácena do fronty, pokud transakce je přerušena. Zpráva se trvale neodebere z fronty, dokud transakce není potvrzena.

Pokud chcete číst zprávu ve frontě, aniž byste ji z fronty odebrali, použijte metodu Peek . Ke zprávě vrácené voláním Peeknení přidružen žádný kontext transakce. Vzhledem k tomu Peek , že neodebere žádné zprávy ve frontě, není nic, co by bylo potřeba vrátit voláním na Abort.

Volání použijte, Receive pokud je přijatelné, aby aktuální vlákno bylo blokováno, zatímco čeká na doručení zprávy do fronty. Vlákno je zablokované po dané časové období nebo neomezeně, pokud jste zadali hodnotu InfiniteTimeout parametru timeout . Pokud by zpracování aplikace mělo pokračovat bez čekání na zprávu, zvažte použití asynchronní metody BeginReceive.

Následující tabulka ukazuje, zda je tato metoda k dispozici v různých režimech pracovní skupiny.

Režim pracovní skupiny K dispozici.
Místní počítač Yes
Název místního počítače a přímého formátu Yes
Vzdálený počítač No
Název vzdáleného počítače a přímého formátu Yes

Viz také

Platí pro

Receive(TimeSpan, Cursor, MessageQueueTransactionType)

Přijme aktuální zprávu ve frontě pomocí zadaného kurzoru. Pokud není k dispozici žádná zpráva, tato metoda počká, dokud nebude zpráva k dispozici nebo časový limit vyprší.

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

Parametry

timeout
TimeSpan

Hodnota TimeSpan označující dobu čekání na zpřístupnění nové zprávy pro kontrolu.

cursor
Cursor

A Cursor , který udržuje určitou pozici ve frontě zpráv.

transactionType
MessageQueueTransactionType

Jedna z MessageQueueTransactionType hodnot, která popisuje typ kontextu transakce, který se má přidružit ke zprávě.

Návraty

A Message , který odkazuje na zprávu ve frontě.

Výjimky

Parametr cursor je null.

Hodnota zadaná pro timeout parametr není platná. Možná timeout je menší než Zero nebo větší než InfiniteTimeout.

Parametr transactionType není jedním ze MessageQueueTransactionType členů.

Zpráva nepřišla do fronty před vypršením časového limitu.

-nebo-

Při přístupu k metodě služby Řízení front zpráv došlo k chybě.

Poznámky

Toto přetížení použijte k přijetí zprávy z fronty pomocí kontextu transakce definovaného transactionType parametrem a vrácení v zadaném časovém období, pokud ve frontě nejsou žádné zprávy.

Zadejte Automatic pro transactionType parametr, pokud již existuje externí transakční kontext připojený k vláknu, které chcete použít k přijetí zprávy. Určete Single , jestli chcete zprávu přijmout jako jednu interní transakci. Můžete určit None , jestli chcete přijímat zprávu z transakční fronty mimo kontext transakce.

Metoda Receive umožňuje synchronní čtení zprávy, čímž ji odebere z fronty. Následná volání pro Receive vrácení zpráv, které následují ve frontě.

Pokud je tato metoda volána k přijetí zprávy z transakční fronty, je přijatá zpráva vrácena do fronty, pokud je transakce přerušena. Zpráva se trvale neodebere z fronty, dokud transakce není potvrzena.

Pokud chcete číst zprávu ve frontě, aniž byste ji z fronty odebrali, použijte metodu Peek . Ke zprávě vrácené voláním Peeknení přidružen žádný kontext transakce. Vzhledem k tomu Peek , že neodebere žádné zprávy ve frontě, není nic, co by bylo potřeba vrátit voláním na Abort.

Volání použijte, Receive pokud je přijatelné, aby aktuální vlákno bylo blokováno, zatímco čeká na doručení zprávy do fronty. Vlákno je zablokované po dané časové období nebo neomezeně, pokud jste zadali hodnotu InfiniteTimeout parametru timeout . Pokud by zpracování aplikace mělo pokračovat bez čekání na zprávu, zvažte použití asynchronní metody BeginReceive.

Následující tabulka ukazuje, zda je tato metoda k dispozici v různých režimech pracovní skupiny.

Režim pracovní skupiny K dispozici.
Místní počítač Yes
Název místního počítače a přímého formátu Yes
Vzdálený počítač No
Název vzdáleného počítače a přímého formátu Yes

Viz také

Platí pro

Bezpečný přístup z více vláken

Metoda není bezpečná pro přístup z více vláken.