使用COM可以获取队列的MessageCount属性。我可以使用MSMQ.Interop实现相同的功能,但我想知道是否有任何方法可以使用纯.NET方法执行此操作?
答案 0 :(得分:0)
当然,使用WMI:
Select * From Win32_PerfRawData_MSMQ_MSMQQueue
答案 1 :(得分:0)
当然!你可以使用LINQ快速而简单的
int Total = 0;
using (var queue = new MessageQueue("queue_name", QueueAccessMode.ReceiveAndAdmin))
{
Total = (from Message msg in queue
select msg).Count();
}
答案 2 :(得分:0)
这是计算私人队列内消息的最快方法。
//using System.Diagnostics
int messageCount =0;
private MessageQueue[] privatequeuelist = MessageQueue.GetPrivateQueuesByMachine(Dns.GetHostName());
foreach(var Queue in MessageQueue)
{
string fullyQualifiedQueueName = string.Format(@"{0}\{1}", Environment.MachineName, Queue.QueueName);
PerformanceCounterCategory category = new PerformanceCounterCategory("MSMQ Queue");
PerformanceCounter cntr = new PerformanceCounter("MSMQ Queue", "Messages in Queue");
if (category.InstanceExists(fullyQualifiedQueueName.ToLower()))
{
cntr.InstanceName = fullyQualifiedQueueName.ToLower(CultureInfo.CurrentCulture);
messageCount = (int)cntr.NextValue();
}
Console.WriteLine(Queue.QueueName + "===" + messageCount);
}
Console.ReadLine();