views:

67

answers:

1

I'm currently doing it like this:

MessageQueue queue = new MessageQueue(".\Private$\myqueue");
MessageEnumerator messageEnumerator = queue.GetMessageEnumerator2();
int i = 0;
while (messageEnumerator.MoveNext())
{
    i++;
}
return i;

But for obvious reasons, it just feels wrong - I shouldn't have to iterate through every message just to get a count, should I?

Is there a better way?

+2  A: 

In C# the answer appears to be no - what you are doing is one of the only ways to do this and all the others are similar.

There are ways to do this using WMI or COM - have a look at the MSMQManagement com component. This has a MessageCount property.


I found the following post that may give you some other ideas for slightly better pure C # implementations:

Counting Messages in an MSMQ MessageQueue from C#

While the above appears to be all true, I should note that I've never tried to do this with MSMQ - I've only ever done standard reading from the queues.

David Hall