tags:

views:

74

answers:

2

I currently use a System.IO.FileSystemWatcher as part of a "roll your own" message queue system (passing XML "messages" between servers using SOAP web services, writing the "messages" to a specific folder on disk where a Windows Service running a FileSystemWatcher pounces on the new "messages" as they arrive and do something productive with them). I wrote this back in the days of .NET 1.0 or 1.1 (I forget which) as an alternative to using COM-interop to whatever the MSMQ SDK was back then. With all of that as background, I'm considering replacing this ad hoc message queuing scheme with a "true" message queuing implementation. Problem is, there doesn't seem to be an MSMQ equivalent to the FileSystemWatcher to automatically pick new messages off of the queue as they arrive. Is it buried somewhere in the .NET framework or am I going to have to either use polling or leave things they way they are for now?

+4  A: 

By calling .Receive in an endless thread, it will block until messages arrived, and return when it gets a message. This is the correct way to do it.

There are also MSMQ Triggers, but you just need the simple .Receive behaviour, AFAICT.

Noon Silk
Yes, MessageQueue.Receive() waits forever, until a message arrives on the queue.
Cheeso
+1  A: 

MessageQueues also have an async receiver:

MessageQueue mq = new MessageQueue();
mq.ReceiveCompleted += new ReceiveCompletedEventHandler(mq_ReceiveCompleted);
mq.BeginReceive();

Just make sure you call BeginReceive() again in the handler, like so:

void mq_ReceiveCompleted(object sender, ReceiveCompletedEventArgs e)
{
  mq.BeginReceive();
  //do work here
}
Jason Z