有没有更好的方法来计数消息队列(MSMQ)中的消息? [英] Is there a better way to count the messages in an Message Queue (MSMQ)?
问题描述
我目前正在这样做:
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?
有更好的方法吗?
推荐答案
在C#中,答案似乎是否定的-您正在执行的操作是执行此操作的唯一方法之一,其他所有方法都相似.
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.
有使用WMI或COM进行此操作的方法-请查看 MSMQManagement com组件.它具有MessageCount属性.
There are ways to do this using WMI or COM - have a look at the MSMQManagement com component. This has a MessageCount property.
我发现以下文章可能会为您提供一些其他一些想法,以使纯C#实现更好一些:
I found the following post that may give you some other ideas for slightly better pure C # implementations:
虽然以上内容看起来都是正确的,但我应该注意,我从未尝试使用MSMQ做到这一点-我只从队列中进行过标准读取.
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.
这篇关于有没有更好的方法来计数消息队列(MSMQ)中的消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!