Focus on Microsoft Technologies - Tutorials, Articles, Code Samples.

Monday, September 11, 2006

MSMQ for .NET Developers, Part 2

art 1 of this article included an example that demonstrated how to read messages from a queue asynchronously. That example worked by first calling BeginReceive on the queue object and then setting a delegate called whenever a new message is received. The BeginReceive method call included a TimeSpan parameter that limited the time to wait for a new message to arrive in the queue to 15 seconds. Using this technique, if a message fails to arrive in the queue within the time allotted, the code calls the ReceiveCompleted delegate—in this case, raising an exception when the EndReceive method is called, signaling the application that no message was received before the timeout expired.


Here is the code used in the original example.

   ' connect to the queue   myReceiveQueue = New _      MessageQueue(".\private$\DevXTestQueue")   myReceiveQueue.Formatter = _      New XmlMessageFormatter(New Type() _      {GetType(System.String)})      ' add a handler to tell MSMQ which method to call when a    ' message arrives   AddHandler myReceiveQueue.ReceiveCompleted, _      AddressOf MSMQ_ReceiveCompleted      ' call the asynchronous version of Receive,    ' waiting no longer than 15 seconds   myReceiveQueue.BeginReceive(New TimeSpan(0, 0, 15))
MSMQ for .NET Developers, Part 2

Post a Comment