using System.Diagnostics;
PerformanceCounter objCounter = new PerformanceCounter("MSMQ Queue", "Messages in Queue", @"mymachineprivate$MyQueue");
int count = (int)(objCounter.NextValue());
//MSMQ性能计数器
PerformanceCounterCategory countCategory = new PerformanceCounterCategory("MSMQ Queue");
//所有消息队列数量
PerformanceCounter allCount = new PerformanceCounter("MSMQ Queue", "Messages in Queue");
foreach (string instanceName in countCategory.GetInstanceNames())
{
allCount.InstanceName = instanceName;//需要给实例名赋值
Console.WriteLine(string.Format("{0} 数量:{1}", allCount.InstanceName, allCount.NextValue().ToString()));
}
//单个消息队列数量
PerformanceCounter singleCount = new PerformanceCounter("MSMQ Queue", "Messages in Queue", Environment.MachineName + "\private$\queue_demo");
singleCount.InstanceName = Environment.MachineName + "\private$\queue_demo";
Console.WriteLine(string.Format("{0} 数量:{1}", singleCount.InstanceName, singleCount.NextValue().ToString()));