15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Programming Message Queuing ❘ 1369<br />

static void Main()<br />

{<br />

var queue = new MessageQueue(@".\Private$\MyPrivateQueue");<br />

queue.Formatter = new XmlMessageFormatter(<br />

new string[] {"System.String"});<br />

}<br />

}<br />

}<br />

Message message = queue.Receive();<br />

Console.WriteLine(message.Body);<br />

code snippet SendMessage/Program.cs<br />

The Receive() message behaves synchronously <strong>and</strong> waits until a message is in the queue if there is none.<br />

enumerating Messages<br />

Instead of reading message by message with the Receive() method, you can use an enumerator to walk<br />

through all messages. The MessageQueue class implements the interface IEnumerable <strong>and</strong> thus can be used<br />

with a foreach statement. Here, the messages are not removed from the queue, but you get just a peek at<br />

the messages to get their content:<br />

var queue = new MessageQueue(@".\Private$\MyPrivateQueue");<br />

queue.Formatter = new XmlMessageFormatter(<br />

new string[] {"System.String"});<br />

foreach (Message message in queue)<br />

{<br />

Console.WriteLine(message.Body);<br />

}<br />

Instead of using the IEnumerable interface, the class MessageEnumerator can be used.<br />

MessageEnumerator implements the interface IEnumerator, but has some more features. With the<br />

IEnumerable interface, the messages are not removed from the queue. The method RemoveCurrent() of<br />

the MessageEnumerator removes the message from the current cursor position of the enumerator.<br />

In the example, the MessageQueue method GetMessageEnumerator() is used to access<br />

the MessageEnumerator. The MoveNext() method takes a peek message by message with the<br />

MessageEnumerator. The MoveNext() method is overloaded to allow a time span as an argument. This is<br />

one of the big advantages when using this enumerator. Here, the thread can wait until a message arrives in<br />

the queue, but only for the specified time span. The Current property, which is defined by the IEnumerator<br />

interface, returns a reference to a message:<br />

asynchronous read<br />

var queue = new MessageQueue(@".\Private$\MyPrivateQueue");<br />

queue.Formatter = new XmlMessageFormatter(<br />

new string[] {"System.String"});<br />

using (MessageEnumerator messages = queue.GetMessageEnumerator())<br />

{<br />

while (messages.MoveNext(TimeSpan.FromMinutes(30)))<br />

{<br />

Message message = messages.Current;<br />

Console.WriteLine(message.Body);<br />

}<br />

}<br />

The Receive method of the MessageQueue class waits until a message from the queue can be read. To<br />

avoid blocking the thread, a timeout can be specified in an overloaded version of the Receive method.<br />

To read the message from the queue after the timeout, Receive() must be invoked again. Instead of polling<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!