MessageQueue.Receive Method

Definition

Receives the first message in the queue, removing it from the queue.

Overloads

Receive()

Receives the first message available in the queue referenced by the MessageQueue. This call is synchronous, and blocks the current thread of execution until a message is available.

Receive(MessageQueueTransaction)

Receives the first message available in the transactional queue referenced by the MessageQueue. This call is synchronous, and blocks the current thread of execution until a message is available.

Receive(MessageQueueTransactionType)

Receives the first message available in the queue referenced by the MessageQueue. This call is synchronous, and blocks the current thread of execution until a message is available.

Receive(TimeSpan)

Receives the first message available in the queue referenced by the MessageQueue and waits until either a message is available in the queue, or the time-out expires.

Receive(TimeSpan, Cursor)

Receives the current message in the queue, using a specified cursor. If no message is available, this method waits until either a message is available, or the time-out expires.

Receive(TimeSpan, MessageQueueTransaction)

Receives the first message available in the transactional queue referenced by the MessageQueue and waits until either a message is available in the queue, or the time-out expires.

Receive(TimeSpan, MessageQueueTransactionType)

Receives the first message available in the queue referenced by the MessageQueue. This call is synchronous, and waits until either a message is available in the queue, or the time-out expires.

Receive(TimeSpan, Cursor, MessageQueueTransaction)

Receives the current message in the queue, using a specified cursor. If no message is available, this method waits until either a message is available, or the time-out expires.

Receive(TimeSpan, Cursor, MessageQueueTransactionType)

Receives the current message in the queue, using a specified cursor. If no message is available, this method waits until either a message is available, or the time-out expires.

Receive()

Receives the first message available in the queue referenced by the MessageQueue. This call is synchronous, and blocks the current thread of execution until a message is available.

C#
public System.Messaging.Message Receive();

Returns

A Message that references the first message available in the queue.

Exceptions

An error occurred when accessing a Message Queuing method.

Examples

The following code example receives a message from a queue and outputs information about that message to the screen.

C#
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;
        }
    }
}

Remarks

Use this overload to receive a message from a queue, or wait until there are messages in the queue.

The Receive method allows for the synchronous reading of a message, thereby removing it from the queue. Subsequent calls to Receive will return the messages that follow in the queue, or new, higher priority messages.

To read the first message in a queue without removing it from the queue, use the Peek method. The Peek method always returns the first message in the queue, so subsequent calls to the method return the same message unless a higher priority message arrives in the queue.

Use a call to Receive when it is acceptable for the current thread to be blocked while it waits for a message to arrive in the queue. Because this overload of the Receive method specifies an infinite time-out, the application might wait indefinitely. If the application processing should continue without waiting for the message, consider using the asynchronous method, BeginReceive.

The following table shows whether this method is available in various Workgroup modes.

Workgroup mode Available
Local computer Yes
Local computer and direct format name Yes
Remote computer No
Remote computer and direct format name Yes

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

Receive(MessageQueueTransaction)

Receives the first message available in the transactional queue referenced by the MessageQueue. This call is synchronous, and blocks the current thread of execution until a message is available.

C#
public System.Messaging.Message Receive(System.Messaging.MessageQueueTransaction transaction);

Parameters

Returns

A Message that references the first message available in the queue.

Exceptions

An error occurred when accessing a Message Queuing method.

-or-

The queue is non-transactional.

Examples

The following code example connects to a transactional queue on the local computer and sends a message to the queue. It then receives the message that contains an order. If it encounters a non-transactional queue, it will throw and exception and rollback the transaction.

C#
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;
        }
    }
}

Remarks

Use this overload to receive a message from a transactional queue using the internal transaction context defined by the transaction parameter, or wait until there are messages in the queue.

The Receive method allows for the synchronous reading of a message, thereby removing it from the queue. Subsequent calls to Receive will return the messages that follow in the queue.

Because this method is called on a transactional queue, the message that is received would be returned to the queue if the transaction is aborted. The message is not permanently removed from the queue until the transaction is committed.

To read the first message in a queue without removing it from the queue, use the Peek method. The Peek method always returns the first message in the queue, so subsequent calls to the method return the same message unless a higher priority message arrives in the queue. There is no transaction context associated with a message returned by a call to Peek. Because Peek does not remove any messages in the queue, there would be nothing to roll back by a call to Abort.

Use a call to Receive when it is acceptable for the current thread to be blocked while it waits for a message to arrive in the queue. Because this overload of the Receive method specifies an infinite time-out, the application might wait indefinitely. If the application processing should continue without waiting for the message, consider using the asynchronous method, BeginReceive.

The following table shows whether this method is available in various Workgroup modes.

Workgroup mode Available
Local computer Yes
Local computer and direct format name Yes
Remote computer No
Remote computer and direct format name Yes

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

Receive(MessageQueueTransactionType)

Receives the first message available in the queue referenced by the MessageQueue. This call is synchronous, and blocks the current thread of execution until a message is available.

C#
public System.Messaging.Message Receive(System.Messaging.MessageQueueTransactionType transactionType);

Parameters

transactionType
MessageQueueTransactionType

One of the MessageQueueTransactionType values, describing the type of transaction context to associate with the message.

Returns

A Message that references the first message available in the queue.

Exceptions

An error occurred when accessing a Message Queuing method.

The transactionType parameter is not one of the MessageQueueTransactionType members.

Examples

The following code example demonstrates the use of Receive(MessageQueueTransactionType).

C#

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

Remarks

Use this overload to receive a message from a queue using a transaction context defined by the transactionType parameter, or wait until there are messages in the queue.

Specify Automatic for the transactionType parameter if there is already an external transaction context attached to the thread that you want to use to receive the message. Specify Single if you want to receive the message as a single internal transaction. You can specify None if you want to receive a message from a transactional queue outside of a transaction context.

The Receive method allows for the synchronous reading of a message, thereby removing it from the queue. Subsequent calls to Receive will return the messages that follow in the queue.

If this method is called to receive a message from a transactional queue, the message that is received would be returned to the queue if the transaction is aborted. The message is not permanently removed from the queue until the transaction is committed.

To read the first message in a queue without removing it from the queue, use the Peek method. The Peek method always returns the first message in the queue, so subsequent calls to the method return the same message unless a higher priority message arrives in the queue. There is no transaction context associated with a message returned by a call to Peek. Because Peek does not remove any messages in the queue, there would be nothing to roll back by a call to Abort.

Use a call to Receive when it is acceptable for the current thread to be blocked while it waits for a message to arrive in the queue. Because this overload of the Receive method specifies an infinite time-out, the application might wait indefinitely. If the application processing should continue without waiting for the message, consider using the asynchronous method, BeginReceive.

The following table shows whether this method is available in various Workgroup modes.

Workgroup mode Available
Local computer Yes
Local computer and direct format name Yes
Remote computer No
Remote computer and direct format name Yes

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

Receive(TimeSpan)

Receives the first message available in the queue referenced by the MessageQueue and waits until either a message is available in the queue, or the time-out expires.

C#
public System.Messaging.Message Receive(TimeSpan timeout);

Parameters

timeout
TimeSpan

A TimeSpan that indicates the time to wait until a new message is available for inspection.

Returns

A Message that references the first message available in the queue.

Exceptions

The value specified for the timeout parameter is not valid, possibly timeout is less than Zero or greater than InfiniteTimeout.

A message did not arrive in the queue before the time-out expired.

-or-

An error occurred when accessing a Message Queuing method

Examples

The following code example receives a message from a queue and outputs information about that message to the screen. The example pauses execution for up to five seconds while waiting for a message to arrive in the queue.

C#
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;
        }
    }
}

Remarks

Use this overload to receive a message and return in a specified period of time if there are no messages in the queue.

The Receive method allows for the synchronous reading of a message, removing it from the queue. Subsequent calls to Receive will return the messages that follow in the queue, or new, higher priority messages.

To read the first message in a queue without removing it from the queue, use the Peek method. The Peek method always returns the first message in the queue, so subsequent calls to the method return the same message unless a higher priority message arrives in the queue.

Use a call to Receive when it is acceptable for the current thread to be blocked while it waits for a message to arrive in the queue. The thread will be blocked for the given period of time, or indefinitely if you specified the value InfiniteTimeout for the timeout parameter. If the application processing should continue without waiting for a message, consider using the asynchronous method, BeginReceive.

The following table shows whether this method is available in various Workgroup modes.

Workgroup mode Available
Local computer Yes
Local computer and direct format name Yes
Remote computer No
Remote computer and direct format name Yes

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

Receive(TimeSpan, Cursor)

Receives the current message in the queue, using a specified cursor. If no message is available, this method waits until either a message is available, or the time-out expires.

C#
public System.Messaging.Message Receive(TimeSpan timeout, System.Messaging.Cursor cursor);

Parameters

timeout
TimeSpan

A TimeSpan that indicates the time to wait until a new message is available for inspection.

cursor
Cursor

A Cursor that maintains a specific position in the message queue.

Returns

A Message that references the first message available in the queue.

Exceptions

The value specified for the timeout parameter is not valid, possibly timeout is less than Zero or greater than InfiniteTimeout.

A message did not arrive in the queue before the time-out expired.

-or-

An error occurred when accessing a Message Queuing method

Use this overload to receive a message and return in a specified period of time if there are no messages in the queue.

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

Receive(TimeSpan, MessageQueueTransaction)

Receives the first message available in the transactional queue referenced by the MessageQueue and waits until either a message is available in the queue, or the time-out expires.

C#
public System.Messaging.Message Receive(TimeSpan timeout, System.Messaging.MessageQueueTransaction transaction);

Parameters

timeout
TimeSpan

A TimeSpan that indicates the time to wait until a new message is available for inspection.

Returns

A Message that references the first message available in the queue.

Exceptions

The value specified for the timeout parameter is not valid, possibly timeout is less than Zero or greater than InfiniteTimeout.

A message did not arrive in the queue before the time-out expired.

-or-

The queue is non-transactional.

-or-

An error occurred when accessing a Message Queuing method.

Examples

The following code example demonstrates the use of this method.

C#
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;
        }
    }
}

Remarks

Use this overload to receive a message from a transactional queue using the internal transaction context defined by the transaction parameter, and return within a specified period of time if there are no messages in the queue.

The Receive method allows for the synchronous reading of a message, thereby removing it from the queue. Subsequent calls to Receive will return the messages that follow in the queue.

Because this method is called on a transactional queue, the message that is received would be returned to the queue if the transaction is aborted. The message is not permanently removed from the queue until the transaction is committed.

To read the first message in a queue without removing it from the queue, use the Peek method. The Peek method always returns the first message in the queue, so subsequent calls to the method return the same message unless a higher priority message arrives in the queue. There is no transaction context associated with a message returned by a call to Peek. Because Peek does not remove any messages in the queue, there would be nothing to roll back by a call to Abort.

Use a call to Receive when it is acceptable for the current thread to be blocked while it waits for a message to arrive in the queue. The thread will be blocked for the given period of time, or indefinitely if you specified the value InfiniteTimeout for the timeout parameter. If the application processing should continue without waiting for a message, consider using the asynchronous method, BeginReceive.

The following table shows whether this method is available in various Workgroup modes.

Workgroup mode Available
Local computer Yes
Local computer and direct format name Yes
Remote computer No
Remote computer and direct format name Yes

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

Receive(TimeSpan, MessageQueueTransactionType)

Receives the first message available in the queue referenced by the MessageQueue. This call is synchronous, and waits until either a message is available in the queue, or the time-out expires.

C#
public System.Messaging.Message Receive(TimeSpan timeout, System.Messaging.MessageQueueTransactionType transactionType);

Parameters

timeout
TimeSpan

A TimeSpan that indicates the time to wait until a new message is available for inspection.

transactionType
MessageQueueTransactionType

One of the MessageQueueTransactionType values, describing the type of transaction context to associate with the message.

Returns

A Message that references the first message available in the queue.

Exceptions

The value specified for the timeout parameter is not valid, possibly timeout is less than Zero or greater than InfiniteTimeout.

The transactionType parameter is not one of the MessageQueueTransactionType members.

A message did not arrive in the queue before the time-out expired.

-or-

An error occurred when accessing a Message Queuing method.

Examples

The following code example demonstrates the use of this method.

C#

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

Remarks

Use this overload to receive a message from a queue using a transaction context defined by the transactionType parameter, and return in a specified period of time if there are no messages in the queue.

Specify Automatic for the transactionType parameter if there is already an external transaction context attached to the thread that you want to use to receive the message. Specify Single if you want to receive the message as a single internal transaction. You can specify None if you want to receive a message from a transactional queue outside of a transaction context.

The Receive method allows for the synchronous reading of a message, thereby removing it from the queue. Subsequent calls to Receive will return the messages that follow in the queue.

If this method is called to receive a message from a transactional queue, the message that is received would be returned to the queue if the transaction is aborted. The message is not permanently removed from the queue until the transaction is committed.

To read the first message in a queue without removing it from the queue, use the Peek method. The Peek method always returns the first message in the queue, so subsequent calls to the method return the same message unless a higher priority message arrives in the queue. There is no transaction context associated with a message returned by a call to Peek. Because Peek does not remove any messages in the queue, there would be nothing to roll back by a call to Abort.

Use a call to Receive when it is acceptable for the current thread to be blocked while it waits for a message to arrive in the queue. The thread will be blocked for the given period of time, or indefinitely if you specified the value InfiniteTimeout for the timeout parameter. If the application processing should continue without waiting for a message, consider using the asynchronous method, BeginReceive.

The following table shows whether this method is available in various Workgroup modes.

Workgroup mode Available
Local computer Yes
Local computer and direct format name Yes
Remote computer No
Remote computer and direct format name Yes

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

Receive(TimeSpan, Cursor, MessageQueueTransaction)

Receives the current message in the queue, using a specified cursor. If no message is available, this method waits until either a message is available, or the time-out expires.

C#
public System.Messaging.Message Receive(TimeSpan timeout, System.Messaging.Cursor cursor, System.Messaging.MessageQueueTransaction transaction);

Parameters

timeout
TimeSpan

A TimeSpan that indicates the time to wait until a new message is available for inspection.

cursor
Cursor

A Cursor that maintains a specific position in the message queue.

Returns

A Message that references a message in the queue.

Exceptions

The cursor parameter is null.

-or-

The transaction parameter is null.

The value specified for the timeout parameter is not valid. Possibly timeout is less than Zero or greater than InfiniteTimeout.

A message did not arrive in the queue before the time-out expired.

-or-

The queue is non-transactional.

-or-

An error occurred when accessing a Message Queuing method.

Remarks

Use this overload to receive a message from a transactional queue using the internal transaction context defined by the transaction parameter, and return within a specified period of time if there are no messages in the queue.

The Receive method allows for the synchronous reading of a message, thereby removing it from the queue. Subsequent calls to Receive return the messages that follow in the queue.

Because this method is called on a transactional queue, the message that is received is returned to the queue if the transaction is aborted. The message is not permanently removed from the queue until the transaction is committed.

To read a message in a queue without removing it from the queue, use the Peek method. There is no transaction context associated with a message returned by a call to Peek. Because Peek does not remove any messages in the queue, there is nothing to roll back by a call to Abort.

Use a call to Receive when it is acceptable for the current thread to be blocked while it waits for a message to arrive in the queue. The thread is blocked for the given period of time, or indefinitely if you specified the value InfiniteTimeout for the timeout parameter. If the application processing should continue without waiting for a message, consider using the asynchronous method, BeginReceive.

The following table shows whether this method is available in various Workgroup modes.

Workgroup mode Available
Local computer Yes
Local computer and direct format name Yes
Remote computer No
Remote computer and direct format name Yes

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

Receive(TimeSpan, Cursor, MessageQueueTransactionType)

Receives the current message in the queue, using a specified cursor. If no message is available, this method waits until either a message is available, or the time-out expires.

C#
public System.Messaging.Message Receive(TimeSpan timeout, System.Messaging.Cursor cursor, System.Messaging.MessageQueueTransactionType transactionType);

Parameters

timeout
TimeSpan

A TimeSpan that indicates the time to wait until a new message is available for inspection.

cursor
Cursor

A Cursor that maintains a specific position in the message queue.

transactionType
MessageQueueTransactionType

One of the MessageQueueTransactionType values that describes the type of transaction context to associate with the message.

Returns

A Message that references a message in the queue.

Exceptions

The cursor parameter is null.

The value specified for the timeout parameter is not valid. Possibly timeout is less than Zero or greater than InfiniteTimeout.

The transactionType parameter is not one of the MessageQueueTransactionType members.

A message did not arrive in the queue before the time-out expired.

-or-

An error occurred when accessing a Message Queuing method.

Remarks

Use this overload to receive a message from a queue using a transaction context defined by the transactionType parameter, and return in a specified period of time if there are no messages in the queue.

Specify Automatic for the transactionType parameter if there is already an external transaction context attached to the thread that you want to use to receive the message. Specify Single if you want to receive the message as a single internal transaction. You can specify None if you want to receive a message from a transactional queue outside of a transaction context.

The Receive method allows for the synchronous reading of a message, thereby removing it from the queue. Subsequent calls to Receive return the messages that follow in the queue.

If this method is called to receive a message from a transactional queue, the message that is received is returned to the queue if the transaction is aborted. The message is not permanently removed from the queue until the transaction is committed.

To read a message in a queue without removing it from the queue, use the Peek method. There is no transaction context associated with a message returned by a call to Peek. Because Peek does not remove any messages in the queue, there is nothing to roll back by a call to Abort.

Use a call to Receive when it is acceptable for the current thread to be blocked while it waits for a message to arrive in the queue. The thread is blocked for the given period of time, or indefinitely if you specified the value InfiniteTimeout for the timeout parameter. If the application processing should continue without waiting for a message, consider using the asynchronous method, BeginReceive.

The following table shows whether this method is available in various Workgroup modes.

Workgroup mode Available
Local computer Yes
Local computer and direct format name Yes
Remote computer No
Remote computer and direct format name Yes

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

Thread Safety

The method is not thread safe.