Export (0) Print
Expand All

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

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

public Message Receive (
	TimeSpan timeout
)
public Message Receive (
	TimeSpan timeout
)
public function Receive (
	timeout : TimeSpan
) : Message

Parameters

timeout

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

Return Value

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

Exception typeCondition

ArgumentException

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

MessageQueueException

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.

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

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.

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

package MyProject;

import System.*;
import System.Messaging.*;

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

/// <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(String[] args)
    {
        // Create a new instance of the class.
        MyNewQueue myNewQueue = new MyNewQueue();
        // Receive a message from a queue.
        myNewQueue.ReceiveMessage();

        return;
    } //main

    //**************************************************
    // 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.set_Formatter(new XmlMessageFormatter(new Type[]
            { MyProject.Order.class.ToType() }));
        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.get_Body());
            // Display message information.
            Console.WriteLine("Order ID: "+((Int32)myOrder.orderId).ToString());
            Console.WriteLine("Sent: " + myOrder.orderTime.ToString());
        }
        catch (MessageQueueException e) {
            // Handle no message arriving in the queue.
            if (e.get_MessageQueueErrorCode().
                Equals(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.get_Message());
        }
        // Catch other exceptions as necessary.
        return;
    } //ReceiveMessage
} //MyNewQueue

  • Full trust for the immediate caller. This member cannot be used by partially trusted code. For more information, see .

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2014 Microsoft