Is there a better way to count the messages in an Message Queue (MSMQ)? [duplicate]
Asked Answered
F

3

7

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?

Fungicide answered 12/4, 2010 at 0:0 Comment(0)
T
4

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.

Tint answered 12/4, 2010 at 0:12 Comment(0)
B
1
            //here queue is msmq queue which you have to find count.        
            int index = 0;
            MSMQManagement msmq = new MSMQManagement() ;   
            object machine = queue.MachineName;
            object path = null;
            object formate=queue.FormatName;
            msmq.Init(ref machine, ref path,ref formate);
            long count = msmq.MessageCount();

This is faster than you selected one. You get MSMQManagement class refferance inside "C:\Program Files (x86)\Microsoft SDKs\Windows" just browse in this address you will get it. for more details you can visit http://msdn.microsoft.com/en-us/library/ms711378%28VS.85%29.aspx.

Blackguard answered 17/6, 2014 at 14:35 Comment(0)
F
-2

The best way to get the count from messageQueue is

MessageQueue queue = new MessageQueue(".\Private$\myqueue");
int iCount = queue.GetAllMessages().count();
Fluoresce answered 8/8, 2011 at 12:29 Comment(2)
The performance of this technique is terrible when the queue is over 10k messages.Alchemize
Furthermore, it might throw OutOfMemoryException if there are too many messages in queue.Pygidium

© 2022 - 2024 — McMap. All rights reserved.