2006/08/25

[C#]用PerformanceCounter讀取MSMQ中的訊息數量

using System.Diagnostics;
using System;

namespace ReadMSMQ
{
    class Program
    {
        static void Main(string[] args)
        {
            //只要改第一與第三個參數, [hostname]\private$\[queuename]
            PerformanceCounter objCounter1 = new PerformanceCounter("MSMQ Queue", "Messages in Queue", @"testHost\private$\testQueue");
            Console.WriteLine(objCounter1.NextValue().ToString());

            //查出MSMQ中, 每個Queue的Messages數量, 可用下面的方式
            PerformanceCounterCategory objCategory = new PerformanceCounterCategory("MSMQ Queue");
            PerformanceCounter objCounter2 = new PerformanceCounter("MSMQ Queue", "Messages in Queue");
            foreach (string strInstanceName in objCategory.GetInstanceNames())
            {
                objCounter2.InstanceName = strInstanceName;
                Console.Write(strInstanceName + " = " + objCounter2.NextValue().ToString());
            }
        }
    }
}

沒有留言: