MessageQueue.Receive Method (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.

Namespace:   System.Messaging
Assembly:  System.Messaging (in System.Messaging.dll)

Public Function Receive (
	timeout As TimeSpan,
	transaction As MessageQueueTransaction
) As Message


Type: System.TimeSpan

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

Type: System.Messaging.MessageQueueTransaction

The MessageQueueTransaction object.

Return Value

Type: System.Messaging.Message

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

Exception Condition

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


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


The queue is non-transactional.


An error occurred when accessing a Message Queuing method.

.NET Framework
Available since 1.1

The method is not thread safe.

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


Local computer


Local computer and direct format name


Remote computer


Remote computer and direct format name


The following code example demonstrates the use of this method.

Imports System
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.

            ' Receive a message from a queue.


        End Sub 'Main

        ' 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.

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

                ' Commit the transaction.

            End If


        End Sub 'SendMessageTransactional

        ' 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() _

            ' Create a transaction.
            Dim myTransaction As New MessageQueueTransaction


                ' Begin the transaction.

                ' 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, _

                ' Display message information.

                ' Commit the transaction.

            Catch e As MessageQueueException

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

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

                    ' 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.

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

            End Try


        End Sub 'ReceiveMessageTransactional

    End Class 'MyNewQueue
End Namespace 'MyProj
Return to top