Showing posts from September, 2010

C# get number of messages in message queue (MSMQ)

Working with message queues in .net is incredibly easy. However I often find myself having to get the number of message in queue for monitoring, etc. That aspect of the message queue framework is a bit lacking. There are a few options available but they all have some problems. You can call GetAllMessages() right off the message queue object. This code works, however it gets a copy of EVERY message queue item currently in the queue. Got 10 items, you get ten. Now do this with 10,000 items every second and it starts to become a problem. It's also possible to enumerate through all the messages in the queue and take a count of them manually but if your queue is busy this can cause invalid cursor exceptions to happen. This is the main reason I moved away from doing this. You can also do some performance counter work but I find that code to be too complex and very fragile and prone to breakage. I've found the best bet is to go with the COM/Win32 route. The nice thing here is