none
How to get the cpu percentage of a specific process using WMI with C# ?

    Question

  • Hi,

    I want to monitor the health of a process on a Windows computer (XP, Server 2003) using WMI, and for this I want to get it's processor usage in percent (same as process listed in the Task Manager with CPU percentage) with using C# (> 2.0).

    I tried naively to use the Win32_PerfFormattedData_PerfOS_Processor classes using the PercentProcessorTime property but this gives me only 0% or 100% values.

    I red on forums that a calculation must be done to get the correct percentage and found this VB script below (http://msdn.microsoft.com/en-us/library/aa394597(VS.85).aspx).

    I tried to apply this code into the following C# code without success by having strange values out for the Timestamp_Sys100NS property (null, big numbers that give wrong calculation).

    Many thanks for your help.



    VB scrip code :

    Set object1 = objWMIService.Get( _
    "Win32_PerfRawData_PerfOS_Processor.Name='_Total'")
    N1 = object1.PercentProcessorTime
    D1 = object1.TimeStamp_Sys100NS
    Wscript.Sleep(1000)
    set object2 = objWMIService.Get( _
    "Win32_PerfRawData_PerfOS_Processor.Name='_Total'")
    N2 = object2.PercentProcessorTime
    D2 = object2.TimeStamp_Sys100NS

    ' CounterType - PERF_100NSEC_TIMER_INV
    ' Formula - (1- ((N2 - N1) / (D2 - D1))) x 100
    PercentProcessorTime = (1 - ((N2 - N1)/(D2-D1)))*100



    C# code :

    mgmtScope = new ManagementScope("\\\\" + hostName + "\\root\\cimv2");
                try {
                    mgmtScope.Connect();
                }

    ...

    string searchQuery = "SELECT * FROM Win32_PerfFormattedData_PerfProc_Process WHERE Name = 'MyProcess'";           
    ObjectQuery oq = new ObjectQuery(searchQuery);
    ManagementObjectSearcher query = new ManagementObjectSearcher(mgmtScope, oq);

    try {
         ManagementObjectCollection servicesCollection = query.Get();              
         foreach (ManagementObject service in servicesCollection) { // seems not possible to do
    servicesCollection[0]
            percentProcessorTime = double.Parse(service.Properties["PercentProcessorTime"].Value.ToString());
         }    
    }
    ...
    string searchQuery = "SELECT * FROM Win32_PerfRawData_PerfOS_Processor WHERE Name = 'MyProcess'";           
    ObjectQuery oq = new ObjectQuery(searchQuery);
    ManagementObjectSearcher query = new ManagementObjectSearcher(mgmtScope, oq);
    try {
       ManagementObjectCollection servicesCollection = query.Get();
       foreach (ManagementObject service in servicesCollection) {
          timeStampSysNS100 = double.Parse(service.Properties["Timestamp_Sys100NS"].Value.ToString());     
       }
    }
    ...

    double d1 = this.getPercentProcessorTime();
    double n1 = this.getTimeStampSysNS100();
    Thread.Sleep(1000);
    double d2 = this.getPercentProcessorTime();
     double n2 = this.getTimeStampSysNS100();

    double percentPT = (1 - ((n2 - n1) / (d2 - d1))) * 100;
    string res = "Process CPU usage : " + percentPT;


    • Moved by jack 321 Friday, September 19, 2008 7:11 AM not C# specific issue (Moved from Visual C# General to Off-Topic Posts (Do Not Post Here))
    Wednesday, September 17, 2008 1:16 PM

All replies