Feature Post


MSMQ/C#: How to notify when a message has arrived?

An excellent help that I found while looking for the same issue. See following:

"Laura" wrote in message
> I will have another app which will
> take the message off the queue and process it. I need code to notify
> me when a message has arrived on the queue.

The answer to that sort of depends on what you mean by "notify".

Perhaps the most basic solution would be something like this

MessageQueue q = new MessageQueue (@".\private$\testq");
Message m = null;

while (true)
m = q.Receive ();
// do something with m

There's no notification per se. The program runs forever (or until an
exception is thrown by Receive). The program blocks inside Receive()
until a message arrives on the queue then Receive pulls the message out
of the queue, returns and sets m to point at the retrieved message.
After you've processed the message, the program loops back and calls
Receive again. If there's already another message in the queue it
returns immediately otherwise it blocks in receive again until another
message shows up.

There are various overloads for Receive that let you do things like
specify a timeout so the Receive will unblock after a period of time
even if no message has shown up yet (but it throws an exception so it
isn't exactly tidy!) More...

If you prefer the event handler approach to C# programming, you can do
that with MSMQ too. To do that you have to create an event handler for
the Receive Completed event, wire that handler up to a queue's
ReceiveCompleted event and then call BeginReceive to start MSMQ looking
for messages. When a message arrives, MSMQ will call fire the
ReceiveCompleted event causing the event handler (or handlers you could
wire up several) to be invoked.

In the event handler, you have to call EndReceive to match the original
BeginReceive and to retrieve your message. Then you process the message
and finally you call BeginReceive again.

You can see an example of that approach here...

There are various other functions that do something similar, look for
ReceiveByXxx functions in the MessageQueue class. More...

There's also something called Peek that gets a copy of a message without
removing it from the queue (but that doesn;t sound like what you want to

There aren't a great deal of examples around for C#. When the MSMQ team
wrote the original samples that was back in the Visual C++ 6.0 and
Visual Basic 6.0 days so that's what most samples are written in. When
the System.Messaging API was released the documentation covers the
basics and shows code snippets but not a lot of complete samples.

This (old) article on the managed API uses Visual Basic but you can
probably pick up the gist of what's going on since the System.Messaging
namespace is the same for VB and C#. More...

This (equally old) article deals with MSMQ over HTTP but the code behind
it shows you how to send and receive messages anyway. More...

Not really relevant but possibly useful in the future is the MSMQ FAQ in MS Word format.