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?
Navigate to 'Computer Management (Local) > Services and Applications > Message Queueing > Private Queues' and verify that the two private queues used by my application are visible.
Microsoft Message Queue server, short MSMQ, provides exactly that - guaranteed and reliable message delivery. It provides an easy way to send messages between different applications or to process messages asynchronously.
On the Edit menu, click Modify. Type amount of space, and then press ENTER. You must specify the amount of space in kilobytes. Note You must restart the Message Queuing resource for these changes to take effect.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With