views:

405

answers:

1

I'm working with message queues in c#. I'm sending messages to a queue from code called by a timer (set up in the main thread), and also from a background worker thread. If I have two instances of the message queue, one for the timer and one for the background worker thread, am I going to face any thread issues? I have had problems when using a single instance of the message queue.

+1  A: 

See MessageQueue on MSDN:

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

As long as you stick to these methods you will be OK (plus any static methods, unless explicitly marked as not thread-safe).

But, MessageQueue instances are reasonably lightweight, so you could just create new instances (maybe via a factory to avoid globals for the queue name) when needed.

Richard