views:

923

answers:

4

I have multiple processes monitoring an MSMQ queue. I want to do multi-step operations like first peek the message and then based on some criteria receive the message. A single message may pass the receiving criteria of multiple processes so that more than one process may try to receive the same message. Will these operations be thread safe? If not what should I do to prevent one process from failing to receive the message that other process has already received?

+4  A: 

The operations themselves are thread safe. However, if you perform a multi-step operation you may find that the results are not consistent (for example, peeking to see if data is in the queue, then calling to receive the data, only to discover it's not there any longer).

Brad Wilson
A: 

Yes it is.

+7  A: 

According to MSDN:

Only the following methods are thread safe: BeginPeek, BeginReceive, EndPeek(IAsyncResult), EndReceive(IAsyncResult), GetAllMessages, Peek, and Receive.

Darksider
+2  A: 

You may be interested in a blog entry I wrote on this subject.

In short, MSMQ C++ API is thread safe, but not all System.Messaging methods are thread safe. In the blog entry I discuss how to call MessageQueue.Send in a thread-safe way.