MessageQueue.Receive Metoda

Definice

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

Přetížení

Name Description
Receive()

Přijme první zprávu dostupnou ve frontě, na kterou odkazuje MessageQueue. Toto volání je synchronní a blokuje aktuální vlákno spuštění, dokud nebude k dispozici zpráva.

Receive(MessageQueueTransaction)

Přijme první zprávu dostupnou v transakční frontě odkazované na MessageQueue. Toto volání je synchronní a blokuje aktuální vlákno spuštění, dokud nebude k dispozici zpráva.

Receive(MessageQueueTransactionType)

Přijme první zprávu dostupnou ve frontě, na kterou odkazuje MessageQueue. Toto volání je synchronní a blokuje aktuální vlákno spuštění, dokud nebude k dispozici zpráva.

Receive(TimeSpan)

Obdrží první zprávu dostupnou ve frontě, na kterou MessageQueue odkazuje, a čeká, dokud ve frontě není dostupná zpráva, nebo vyprší časový limit.

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 k dispozici zpráva nebo vyprší časový limit.

Receive(TimeSpan, MessageQueueTransaction)

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

Receive(TimeSpan, MessageQueueTransactionType)

Přijme první zprávu dostupnou ve frontě, na kterou odkazuje MessageQueue. 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 k dispozici zpráva nebo vyprší časový limit.

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 k dispozici zpráva nebo vyprší časový limit.

Receive()

Přijme první zprávu dostupnou ve frontě, na kterou odkazuje MessageQueue. Toto volání je synchronní a blokuje aktuální vlákno spuštění, dokud nebude 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ě řízení front zpráv došlo k chybě.

Příklady

Následující příklad kódu obdrží 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ávy z fronty nebo počkejte, dokud ve frontě nejsou zprávy.

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

Pokud chcete přečíst první zprávu ve frontě, aniž byste ji odebrali z fronty, 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 nepřijde zpráva s vyšší prioritou.

Receive Volání použijte, pokud je přijatelné, aby aktuální vlákno bylo bloková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, může aplikace č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 Available
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)

Přijme první zprávu dostupnou v transakční frontě odkazované na MessageQueue. Toto volání je synchronní a blokuje aktuální vlákno spuštění, dokud nebude 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ě ří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. Poté obdrží zprávu, která obsahuje objednávku. Pokud narazí na neaktuální frontu, vyvolá se výjimka a vrácení transakce zpět.

#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

Poznámky

Toto přetížení použijte k příjmu zprávy z transakční fronty pomocí interního kontextu transakce definovaného transaction parametrem nebo počkejte, dokud ve frontě nejsou zprávy.

Metoda Receive umožňuje synchronní čtení zprávy, čímž ji odeberete z fronty. Následná volání 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 přijatá by byla vrácena do fronty, pokud je transakce přerušena. Zpráva není trvale odebrána z fronty, dokud transakce nebude potvrzena.

Pokud chcete přečíst první zprávu ve frontě, aniž byste ji odebrali z fronty, 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 nepřijde zpráva s vyšší prioritou. Neexistuje žádný kontext transakce spojený se zprávou vrácenou voláním Peek. Vzhledem k tomu Peek , že neodebere žádné zprávy ve frontě, nebude možné vrátit zpět voláním Abort.

Receive Volání použijte, pokud je přijatelné, aby aktuální vlákno bylo bloková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, může aplikace č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 Available
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)

Přijme první zprávu dostupnou ve frontě, na kterou odkazuje MessageQueue. Toto volání je synchronní a blokuje aktuální vlákno spuštění, dokud nebude 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ě řízení front zpráv došlo k chybě.

Parametr transactionType není jedním z 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říjmu zprávy z fronty pomocí kontextu transakce definovaného transactionType parametrem nebo počkejte, dokud ve frontě nejsou zprávy.

Zadejte Automatic parametr transactionType , 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řijmout zprávu z transakční fronty mimo kontext transakce.

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

Pokud je tato metoda volána pro příjem zprávy z transakční fronty, zpráva přijatá by byla vrácena do fronty, pokud je transakce přerušena. Zpráva není trvale odebrána z fronty, dokud transakce nebude potvrzena.

Pokud chcete přečíst první zprávu ve frontě, aniž byste ji odebrali z fronty, 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 nepřijde zpráva s vyšší prioritou. Neexistuje žádný kontext transakce spojený se zprávou vrácenou voláním Peek. Vzhledem k tomu Peek , že neodebere žádné zprávy ve frontě, nebude možné vrátit zpět voláním Abort.

Receive Volání použijte, pokud je přijatelné, aby aktuální vlákno bylo bloková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, může aplikace č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 Available
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)

Obdrží první zprávu dostupnou ve frontě, na kterou MessageQueue odkazuje, a čeká, dokud ve frontě není dostupná zpráva, nebo vyprší časový limit.

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

A TimeSpan , který označuje dobu čekání, až 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ší nebo Zero větší než InfiniteTimeout.

Zpráva nebyla doručena do fronty před vypršením časového limitu.

nebo

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

Příklady

Následující příklad kódu obdrží zprávu z fronty a vypíše informace o této zprávě na obrazovku. Příklad pozastaví provádění až pět sekund a čeká 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.

Tato Receive metoda umožňuje synchronní čtení zprávy a odebrání zprávy z fronty. Následná volání Receive vrátí zprávy, které následují ve frontě, nebo nové zprávy s vyšší prioritou.

Pokud chcete přečíst první zprávu ve frontě, aniž byste ji odebrali z fronty, 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 nepřijde zpráva s vyšší prioritou.

Receive Volání použijte, 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é pro dané časové období nebo po 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 Available
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 k dispozici zpráva nebo vyprší časový limit.

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

A TimeSpan , který označuje dobu čekání, až 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ší nebo Zero větší než InfiniteTimeout.

Zpráva nebyla doručena do fronty před vypršením časového limitu.

nebo

Při přístupu k metodě ří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 čeká, dokud ve frontě není k dispozici zpráva, nebo vyprší č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

A TimeSpan , který označuje dobu čekání, až 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ší nebo Zero větší než InfiniteTimeout.

Zpráva nebyla doručena do fronty před vypršením časového limitu.

nebo

Fronta není transakční.

nebo

Při přístupu k metodě ří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)
      {
         // 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

Poznámky

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

Metoda Receive umožňuje synchronní čtení zprávy, čímž ji odeberete z fronty. Následná volání 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 přijatá by byla vrácena do fronty, pokud je transakce přerušena. Zpráva není trvale odebrána z fronty, dokud transakce nebude potvrzena.

Pokud chcete přečíst první zprávu ve frontě, aniž byste ji odebrali z fronty, 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 nepřijde zpráva s vyšší prioritou. Neexistuje žádný kontext transakce spojený se zprávou vrácenou voláním Peek. Vzhledem k tomu Peek , že neodebere žádné zprávy ve frontě, nebude možné vrátit zpět voláním Abort.

Receive Volání použijte, 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é pro dané časové období nebo po 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 Available
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)

Přijme první zprávu dostupnou ve frontě, na kterou odkazuje MessageQueue. 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

A TimeSpan , který označuje dobu čekání, až bude k dispozici nová zpráva 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ší nebo Zero větší než InfiniteTimeout.

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

Zpráva nebyla doručena do fronty před vypršením časového limitu.

nebo

Při přístupu k metodě ří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říjmu zprávy z fronty pomocí kontextu transakce definovaného transactionType parametrem a vrátit se v zadaném časovém období, pokud ve frontě nejsou žádné zprávy.

Zadejte Automatic parametr transactionType , 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řijmout zprávu z transakční fronty mimo kontext transakce.

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

Pokud je tato metoda volána pro příjem zprávy z transakční fronty, zpráva přijatá by byla vrácena do fronty, pokud je transakce přerušena. Zpráva není trvale odebrána z fronty, dokud transakce nebude potvrzena.

Pokud chcete přečíst první zprávu ve frontě, aniž byste ji odebrali z fronty, 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 nepřijde zpráva s vyšší prioritou. Neexistuje žádný kontext transakce spojený se zprávou vrácenou voláním Peek. Vzhledem k tomu Peek , že neodebere žádné zprávy ve frontě, nebude možné vrátit zpět voláním Abort.

Receive Volání použijte, 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é pro dané časové období nebo po 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 Available
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

A TimeSpan , který označuje dobu čekání, až 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 zprávu ve frontě.

Výjimky

Parametr cursor je null.

nebo

Parametr transaction je null.

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

Zpráva nebyla doručena do fronty před vypršením časového limitu.

nebo

Fronta není transakční.

nebo

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

Poznámky

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

Metoda Receive umožňuje synchronní čtení zprávy, čímž ji odeberete 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ě, zpráva, která je přijata, je vrácena do fronty, pokud je transakce přerušena. Zpráva není trvale odebrána z fronty, dokud transakce nebude potvrzena.

Pokud chcete přečíst zprávu ve frontě, aniž byste ji odebrali z fronty, použijte metodu Peek . Neexistuje žádný kontext transakce spojený se zprávou vrácenou voláním Peek. Protože Peek neodebere žádné zprávy ve frontě, není nic vrátit zpět voláním Abort.

Receive Volání použijte, 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é pro dané časové období nebo neomezeně dlouho, 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 Available
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 k dispozici zpráva nebo vyprší časový limit.

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

A TimeSpan , který označuje dobu čekání, až bude k dispozici nová zpráva pro kontrolu.

cursor
Cursor

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

transactionType
MessageQueueTransactionType

Jedna z MessageQueueTransactionType hodnot, které popisují typ kontextu transakce 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 parametr timeout není platná. Možná timeout je menší než Zero nebo větší než InfiniteTimeout.

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

Zpráva nebyla doručena do fronty před vypršením časového limitu.

nebo

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

Poznámky

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

Zadejte Automatic parametr transactionType , 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řijmout zprávu z transakční fronty mimo kontext transakce.

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

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

Pokud chcete přečíst zprávu ve frontě, aniž byste ji odebrali z fronty, použijte metodu Peek . Neexistuje žádný kontext transakce spojený se zprávou vrácenou voláním Peek. Protože Peek neodebere žádné zprávy ve frontě, není nic vrátit zpět voláním Abort.

Receive Volání použijte, 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é pro dané časové období nebo neomezeně dlouho, 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 Available
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.