Export (0) Print
Expand All
0 out of 1 rated this helpful - Rate this topic

MessageQueue Class

Provides access to a queue on a Message Queuing server.

Namespace:  System.Messaging
Assembly:  System.Messaging (in System.Messaging.dll)
[MessagingDescriptionAttribute("MessageQueueDesc")]
public class MessageQueue : Component, 
	IEnumerable

The MessageQueue type exposes the following members.

  NameDescription
Public methodMessageQueue()Initializes a new instance of the MessageQueue class. After the default constructor initializes the new instance, you must set the instance's Path property before you can use the instance.
Public methodMessageQueue(String)Initializes a new instance of the MessageQueue class that references the Message Queuing queue at the specified path.
Public methodMessageQueue(String, Boolean)Initializes a new instance of the MessageQueue class that references the Message Queuing queue at the specified path and with the specified read-access restriction.
Public methodMessageQueue(String, QueueAccessMode)Initializes a new instance of the MessageQueue class.
Public methodMessageQueue(String, Boolean, Boolean)Initializes a new instance of the MessageQueue class.
Public methodMessageQueue(String, Boolean, Boolean, QueueAccessMode)Initializes a new instance of the MessageQueue class.
Top
  NameDescription
Public propertyAccessModeGets a value that indicates the access mode for the queue.
Public propertyAuthenticateGets or sets a value that indicates whether the queue accepts only authenticated messages.
Public propertyBasePriorityGets or sets the base priority Message Queuing uses to route a public queue's messages over the network.
Protected propertyCanRaiseEventsGets a value indicating whether the component can raise an event. (Inherited from Component.)
Public propertyCanReadGets a value that indicates whether the MessageQueue can be read.
Public propertyCanWriteGets a value that indicates whether the MessageQueue can be written to.
Public propertyCategoryGets or sets the queue category.
Public propertyContainerGets the IContainer that contains the Component. (Inherited from Component.)
Public propertyCreateTimeGets the time and date that the queue was created in Message Queuing.
Public propertyDefaultPropertiesToSendGets or sets the message property values to be used by default when the application sends messages to the queue.
Public propertyDenySharedReceiveGets or sets a value that indicates whether this MessageQueue has exclusive access to receive messages from the Message Queuing queue.
Protected propertyDesignModeGets a value that indicates whether the Component is currently in design mode. (Inherited from Component.)
Public propertyStatic memberEnableConnectionCacheGets or sets a value that indicates whether a cache of connections will be maintained by the application.
Public propertyEncryptionRequiredGets or sets a value that indicates whether the queue accepts only non-private (non-encrypted) messages.
Protected propertyEventsGets the list of event handlers that are attached to this Component. (Inherited from Component.)
Public propertyFormatNameGets the unique queue name that Message Queuing generated at the time of the queue's creation.
Public propertyFormatterGets or sets the formatter used to serialize an object into or deserialize an object from the body of a message read from or written to the queue.
Public propertyIdGets the unique Message Queuing identifier of the queue.
Public propertyLabelGets or sets the queue description.
Public propertyLastModifyTimeGets the last time the properties of a queue were modified.
Public propertyMachineNameGets or sets the name of the computer where the Message Queuing queue is located.
Public propertyMaximumJournalSizeGets or sets the maximum size of the journal queue.
Public propertyMaximumQueueSizeGets or sets the maximum size of the queue.
Public propertyMessageReadPropertyFilterGets or sets the property filter for receiving or peeking messages.
Public propertyMulticastAddressIntroduced in MSMQ 3.0. Gets or sets the multicast address associated with the queue.
Public propertyPathGets or sets the queue's path. Setting the Path causes the MessageQueue to point to a new queue.
Public propertyQueueNameGets or sets the friendly name that identifies the queue.
Public propertyReadHandleGets the native handle used to read messages from the message queue.
Public propertySiteGets or sets the ISite of the Component. (Inherited from Component.)
Public propertySynchronizingObjectGets or sets the object that marshals the event-handler call resulting from a ReceiveCompleted or PeekCompleted event.
Public propertyTransactionalGets a value that indicates whether the queue accepts only transactions.
Public propertyUseJournalQueueGets or sets a value that indicates whether received messages are copied to the journal queue.
Public propertyWriteHandleGets the native handle used to send messages to the message queue.
Top
  NameDescription
Public methodBeginPeek()Initiates an asynchronous peek operation that has no time-out. The operation is not complete until a message becomes available in the queue.
Public methodBeginPeek(TimeSpan)Initiates an asynchronous peek operation that has a specified time-out. The operation is not complete until either a message becomes available in the queue or the time-out occurs.
Public methodBeginPeek(TimeSpan, Object)Initiates an asynchronous peek operation that has a specified time-out and a specified state object, which provides associated information throughout the operation's lifetime. The operation is not complete until either a message becomes available in the queue or the time-out occurs.
Public methodBeginPeek(TimeSpan, Object, AsyncCallback)Initiates an asynchronous peek operation that has a specified time-out and a specified state object, which provides associated information throughout the operation's lifetime. This overload receives notification, through a callback, of the identity of the event handler for the operation. The operation is not complete until either a message becomes available in the queue or the time-out occurs.
Public methodBeginPeek(TimeSpan, Cursor, PeekAction, Object, AsyncCallback)Initiates an asynchronous peek operation that has a specified time-out and that uses a specified cursor, a specified peek action, and a specified state object. The state object provides associated information throughout the lifetime of the operation. This overload receives notification, through a callback, of the identity of the event handler for the operation. The operation is not complete until either a message becomes available in the queue or the time-out occurs.
Public methodBeginReceive()Initiates an asynchronous receive operation that has no time-out. The operation is not complete until a message becomes available in the queue.
Public methodBeginReceive(TimeSpan)Initiates an asynchronous receive operation that has a specified time-out. The operation is not complete until either a message becomes available in the queue or the time-out occurs.
Public methodBeginReceive(TimeSpan, Object)Initiates an asynchronous receive operation that has a specified time-out and a specified state object, which provides associated information throughout the operation's lifetime. The operation is not complete until either a message becomes available in the queue or the time-out occurs.
Public methodBeginReceive(TimeSpan, Object, AsyncCallback)Initiates an asynchronous receive operation that has a specified time-out and a specified state object, which provides associated information throughout the operation's lifetime. This overload receives notification, through a callback, of the identity of the event handler for the operation. The operation is not complete until either a message becomes available in the queue or the time-out occurs.
Public methodBeginReceive(TimeSpan, Cursor, Object, AsyncCallback)Initiates an asynchronous receive operation that has a specified time-out and uses a specified cursor and a specified state object. The state object provides associated information throughout the lifetime of the operation. This overload receives notification, through a callback, of the identity of the event handler for the operation. The operation is not complete until either a message becomes available in the queue or the time-out occurs.
Public methodStatic memberClearConnectionCacheClears the connection cache.
Public methodCloseFrees all resources allocated by the MessageQueue.
Public methodStatic memberCreate(String)Creates a non-transactional Message Queuing queue at the specified path.
Public methodStatic memberCreate(String, Boolean)Creates a transactional or non-transactional Message Queuing queue at the specified path.
Public methodCreateCursorCreates a new Cursor for the current message queue.
Public methodCreateObjRefCreates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object. (Inherited from MarshalByRefObject.)
Public methodStatic memberDeleteDeletes a queue on a Message Queuing server.
Public methodDispose()Releases all resources used by the Component. (Inherited from Component.)
Protected methodDispose(Boolean)Disposes of the resources (other than memory) used by the MessageQueue. (Overrides Component.Dispose(Boolean).)
Public methodEndPeekCompletes the specified asynchronous peek operation.
Public methodEndReceiveCompletes the specified asynchronous receive operation.
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Public methodStatic memberExistsDetermines whether a Message Queuing queue exists at the specified path.
Protected methodFinalizeReleases unmanaged resources and performs other cleanup operations before the Component is reclaimed by garbage collection. (Inherited from Component.)
Public methodGetAllMessagesReturns all the messages that are in the queue.
Public methodGetEnumerator Obsolete. Enumerates the messages in a queue. GetEnumerator is deprecated. GetMessageEnumerator2 should be used instead.
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetLifetimeServiceRetrieves the current lifetime service object that controls the lifetime policy for this instance. (Inherited from MarshalByRefObject.)
Public methodStatic memberGetMachineIdGets the identifier of the computer on which the queue referenced by this MessageQueue is located.
Public methodGetMessageEnumerator Obsolete. Creates an enumerator object for all the messages in the queue. GetMessageEnumerator is deprecated. GetMessageEnumerator2 should be used instead.
Public methodGetMessageEnumerator2Creates an enumerator object for all the messages in the queue.
Public methodStatic memberGetMessageQueueEnumerator()Provides forward-only cursor semantics to enumerate through all public queues on the network.
Public methodStatic memberGetMessageQueueEnumerator(MessageQueueCriteria)Provides forward-only cursor semantics to enumerate through all public queues on the network that meet the specified criteria.
Public methodStatic memberGetPrivateQueuesByMachineRetrieves all the private queues on the specified computer.
Public methodStatic memberGetPublicQueues()Retrieves all the public queues on the network.
Public methodStatic memberGetPublicQueues(MessageQueueCriteria)Retrieves all the public queues on the network that meet the specified criteria.
Public methodStatic memberGetPublicQueuesByCategoryRetrieves all the public queues on the network that belong to the specified category.
Public methodStatic memberGetPublicQueuesByLabelRetrieves all the public queues on the network that carry the specified label.
Public methodStatic memberGetPublicQueuesByMachineRetrieves all the public queues that reside on the specified computer.
Public methodStatic memberGetSecurityContextRetrieves the security context that MSMQ associates with the current user (thread identity) at the time of this call.
Protected methodGetServiceReturns an object that represents a service provided by the Component or by its Container. (Inherited from Component.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodInitializeLifetimeServiceObtains a lifetime service object to control the lifetime policy for this instance. (Inherited from MarshalByRefObject.)
Protected methodMemberwiseClone()Creates a shallow copy of the current Object. (Inherited from Object.)
Protected methodMemberwiseClone(Boolean)Creates a shallow copy of the current MarshalByRefObject object. (Inherited from MarshalByRefObject.)
Public methodPeek()Returns without removing (peeks) the first message in the queue referenced by this MessageQueue. The Peek method is synchronous, so it blocks the current thread until a message becomes available.
Public methodPeek(TimeSpan)Returns without removing (peeks) the first message in the queue referenced by this MessageQueue. The Peek method is synchronous, so it blocks the current thread until a message becomes available or the specified time-out occurs.
Public methodPeek(TimeSpan, Cursor, PeekAction)Returns without removing (peeks) the current or next message in the queue, using the specified cursor. The Peek method is synchronous, so it blocks the current thread until a message becomes available or the specified time-out occurs.
Public methodPeekByCorrelationId(String)Peeks the message that matches the given correlation identifier and immediately raises an exception if no message with the specified correlation identifier currently exists in the queue.
Public methodPeekByCorrelationId(String, TimeSpan)Peeks the message that matches the given correlation identifier and waits until either a message with the specified correlation identifier is available in the queue, or the time-out expires.
Public methodPeekById(String)Peeks the message whose message identifier matches the id parameter.
Public methodPeekById(String, TimeSpan)Peeks the message whose message identifier matches the id parameter. Waits until the message appears in the queue or a time-out occurs.
Public methodPeekByLookupId(Int64)Introduced in MSMQ 3.0. Peeks at the message that matches the given lookup identifier from a non-transactional queue.
Public methodPeekByLookupId(MessageLookupAction, Int64)Introduced in MSMQ 3.0. Peeks at a specific message from the queue. The message can be specified by a lookup identifier or by its position at the front or end of the queue.
Public methodPurgeDeletes all the messages contained in the queue.
Public methodReceive()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.
Public methodReceive(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.
Public methodReceive(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.
Public methodReceive(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.
Public methodReceive(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.
Public methodReceive(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.
Public methodReceive(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.
Public methodReceive(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.
Public methodReceive(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.
Public methodReceiveByCorrelationId(String)Receives the message that matches the given correlation identifier (from a non-transactional queue) and immediately raises an exception if no message with the specified correlation identifier currently exists in the queue.
Public methodReceiveByCorrelationId(String, MessageQueueTransaction)Receives the message that matches the given correlation identifier (from a transactional queue) and immediately raises an exception if no message with the specified correlation identifier currently exists in the queue.
Public methodReceiveByCorrelationId(String, MessageQueueTransactionType)Receives the message that matches the given correlation identifier and immediately raises an exception if no message with the specified correlation identifier currently exists in the queue.
Public methodReceiveByCorrelationId(String, TimeSpan)Receives the message that matches the given correlation identifier (from a non-transactional queue) and waits until either a message with the specified correlation identifier is available in the queue, or the time-out expires.
Public methodReceiveByCorrelationId(String, TimeSpan, MessageQueueTransaction)Receives the message that matches the given correlation identifier (from a transactional queue) and waits until either a message with the specified correlation identifier is available in the queue, or the time-out expires.
Public methodReceiveByCorrelationId(String, TimeSpan, MessageQueueTransactionType)Receives the message that matches the given correlation identifier and waits until either a message with the specified correlation identifier is available in the queue, or the time-out expires.
Public methodReceiveById(String)Receives the message that matches the given identifier from a non-transactional queue and immediately raises an exception if no message with the specified identifier currently exists in the queue.
Public methodReceiveById(String, MessageQueueTransaction)Receives the message that matches the given identifier (from a transactional queue) and immediately raises an exception if no message with the specified identifier currently exists in the queue.
Public methodReceiveById(String, MessageQueueTransactionType)Receives the message that matches the given identifier and immediately raises an exception if no message with the specified identifier currently exists in the queue.
Public methodReceiveById(String, TimeSpan)Receives the message that matches the given identifier (from a non-transactional queue) and waits until either a message with the specified identifier is available in the queue or the time-out expires.
Public methodReceiveById(String, TimeSpan, MessageQueueTransaction)Receives the message that matches the given identifier (from a transactional queue) and waits until either a message with the specified identifier is available in the queue or the time-out expires.
Public methodReceiveById(String, TimeSpan, MessageQueueTransactionType)Receives the message that matches the given identifier and waits until either a message with the specified identifier is available in the queue or the time-out expires.
Public methodReceiveByLookupId(Int64)Introduced in MSMQ 3.0. Receives the message that matches the given lookup identifier from a non-transactional queue.
Public methodReceiveByLookupId(MessageLookupAction, Int64, MessageQueueTransaction)Introduced in MSMQ 3.0. Receives a specific message from a transactional queue. The message can be specified by a lookup identifier or by its position at the front or end of the queue.
Public methodReceiveByLookupId(MessageLookupAction, Int64, MessageQueueTransactionType)Introduced in MSMQ 3.0. Receives a specific message from the queue, using the specified transaction context. The message can be specified by a lookup identifier or by its position at the front or end of the queue.
Public methodRefreshRefreshes the properties presented by the MessageQueue to reflect the current state of the resource.
Public methodResetPermissionsResets the permission list to the operating system's default values. Removes any queue permissions you have appended to the default list.
Public methodSend(Object)Sends an object to non-transactional queue referenced by this MessageQueue.
Public methodSend(Object, MessageQueueTransaction)Sends an object to the transactional queue referenced by this MessageQueue.
Public methodSend(Object, MessageQueueTransactionType)Sends an object to the queue referenced by this MessageQueue.
Public methodSend(Object, String)Sends an object to the non-transactional queue referenced by this MessageQueue and specifies a label for the message.
Public methodSend(Object, String, MessageQueueTransaction)Sends an object to the transactional queue referenced by this MessageQueue and specifies a label for the message.
Public methodSend(Object, String, MessageQueueTransactionType)Sends an object to the queue referenced by this MessageQueue and specifies a label for the message.
Public methodSetPermissions(AccessControlList)Assigns access rights to the queue based on the contents of an access control list.
Public methodSetPermissions(MessageQueueAccessControlEntry)Assigns access rights to the queue based on the contents of an access control entry.
Public methodSetPermissions(String, MessageQueueAccessRights)Gives a computer, group, or user the specified access rights.
Public methodSetPermissions(String, MessageQueueAccessRights, AccessControlEntryType)Gives a computer, group, or user the specified access rights, with the specified access control type (allow, deny, revoke, or set).
Public methodToStringReturns a String containing the name of the Component, if any. This method should not be overridden. (Inherited from Component.)
Top
  NameDescription
Public eventDisposedOccurs when the component is disposed by a call to the Dispose method. (Inherited from Component.)
Public eventPeekCompletedOccurs when a message is read without being removed from the queue. This is a result of the asynchronous operation, BeginPeek.
Public eventReceiveCompletedOccurs when a message has been removed from the queue. This event is raised by the asynchronous operation, BeginReceive.
Top
  NameDescription
Public Extension MethodAsParallelEnables parallelization of a query. (Defined by ParallelEnumerable.)
Public Extension MethodAsQueryableConverts an IEnumerable to an IQueryable. (Defined by Queryable.)
Public Extension MethodCast<TResult>Casts the elements of an IEnumerable to the specified type. (Defined by Enumerable.)
Public Extension MethodOfType<TResult>Filters the elements of an IEnumerable based on a specified type. (Defined by Enumerable.)
Top
  NameDescription
Public fieldStatic memberInfiniteQueueSizeSpecifies that no size restriction exists for a queue.
Public fieldStatic memberInfiniteTimeoutSpecifies that no time-out exists for methods that peek or receive messages.
Top

The Message Queuing technology allows applications running at different times to communicate across heterogeneous networks and systems that might be temporarily offline. Applications send, receive, or peek (read without removing) messages from queues. Message Queuing is an optional component of Windows 2000 and Windows NT, and must be installed separately.

The MessageQueue class is a wrapper around Message Queuing. There are multiple versions of Message Queuing, and using the MessageQueue class can result in slightly different behavior, depending on the operating system you are using. For information about specific features of each version of Message Queuing, see the topic "What's New in Message Queuing" in the Platform SDK in MSDN.

The MessageQueue class provides a reference to a Message Queuing queue. You can specify a path in the MessageQueue constructor to connect to an existing resource, or you can create a new queue on the server. Before you can call Send(Object), Peek, or Receive, you must associate the new instance of the MessageQueue class with an existing queue. At that point, you can manipulate the queue properties such as Category and Label.

MessageQueue supports two types of message retrieval: synchronous and asynchronous. The synchronous methods, Peek and Receive, cause the process thread to wait a specified time interval for a new message to arrive in the queue. The asynchronous methods, BeginPeek and BeginReceive, allow the main application tasks to continue in a separate thread until a message arrives in the queue. These methods work by using callback objects and state objects to communicate information between threads.

When you create a new instance of the MessageQueue class, you are not creating a new Message Queuing queue. Instead, you can use the Create(String), Delete(String), and Purge methods to manage queues on the server.

Unlike Purge, Create(String) and Delete(String) are static members, so you can call them without creating a new instance of the MessageQueue class.

You can set the MessageQueue object's Path property with one of three names: the friendly name, the FormatName, or the Label. The friendly name, which is defined by the queue's MachineName and QueueName properties, is MachineName\QueueName for a public queue, and MachineName\Private$\QueueName for a private queue. The FormatName property allows offline access to message queues. Lastly, you can use the queue's Label property to set the queue's Path.

For a list of initial property values for an instance of MessageQueue, see the MessageQueue constructor.

The following code example creates new MessageQueue objects using various path name syntax types. In each case, it sends a message to the queue whose path is defined in the constructor.

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 demonstrates several ways to set 
		// a queue's path. 
		//************************************************** 

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

			myNewQueue.SendPublic();
			myNewQueue.SendPrivate();
			myNewQueue.SendByLabel();
			myNewQueue.SendByFormatName();
			myNewQueue.MonitorComputerJournal();
			myNewQueue.MonitorQueueJournal();
			myNewQueue.MonitorDeadLetter();
			myNewQueue.MonitorTransactionalDeadLetter();

			return;
		}
		
		// References public queues. 
		public void SendPublic()
		{
			MessageQueue myQueue = new MessageQueue(".\\myQueue");
			myQueue.Send("Public queue by path name.");

			return;
		}

		// References private queues. 
		public void SendPrivate()
		{
			MessageQueue myQueue = new 
				MessageQueue(".\\Private$\\myQueue");
			myQueue.Send("Private queue by path name.");

			return;
		}

		// References queues by label. 
		public void SendByLabel()
		{
			MessageQueue myQueue = new MessageQueue("Label:TheLabel");
			myQueue.Send("Queue by label.");

			return;
		}

		// References queues by format name. 
		public void SendByFormatName()
		{
			MessageQueue myQueue = new 
				MessageQueue("FormatName:Public=5A5F7535-AE9A-41d4" + 
				"-935C-845C2AFF7112");
			myQueue.Send("Queue by format name.");

			return;
		}

		// References computer journal queues. 
		public void MonitorComputerJournal()
		{
			MessageQueue computerJournal = new 
				MessageQueue(".\\Journal$");
			while(true)
			{
				Message journalMessage = computerJournal.Receive();
				// Process the journal message.
			}
		}

		// References queue journal queues. 
		public void MonitorQueueJournal()
		{
			MessageQueue queueJournal = new 
				MessageQueue(".\\myQueue\\Journal$");
			while(true)
			{
				Message journalMessage = queueJournal.Receive();
				// Process the journal message.
			}
		}
		
		// References dead-letter queues. 
		public void MonitorDeadLetter()
		{
			MessageQueue deadLetter = new 
				MessageQueue(".\\DeadLetter$");
			while(true)
			{
				Message deadMessage = deadLetter.Receive();
				// Process the dead-letter message.
			}
		}

		// References transactional dead-letter queues. 
		public void MonitorTransactionalDeadLetter()
		{
			MessageQueue TxDeadLetter = new 
				MessageQueue(".\\XactDeadLetter$");
			while(true)
			{
				Message txDeadLetter = TxDeadLetter.Receive();
				// Process the transactional dead-letter message.
			}
		}

	}
}

The following code example sends a message to a queue, and receives a message from a queue, using an application-specific class called Order.

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

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Only the GetAllMessages method is thread safe.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.