locked
Code using Win32_PerfFormattedData_PerfProc_Process works in Vista and not in Windows 2003 server. RRS feed

  • Question

  • Hi all,

    I am attempting to capture processor time used by a process using some sample C# code.

    The code sample is at the bottom of the post. I am running the code against the local machine and not a remote machine.

    It seems to work fine in Vista but when on Windows 2003 server I can't seem get a non-zero value for processor time. Any clues?

    I've tried running the same executable on several Windows 2003 systems. Also I have tried compiling the code on Windows 2003 and running the executable on the Windows 2003 systems. I am running the code as user administrator to remove any issues with security.

     

    Here is output on Vista...

     

    CreatingProcessID : 3112
    Proc Time : 0
    CreatingProcessID : 3112
    Proc Time : 0
    CreatingProcessID : 6088
    Proc Time : 0
    CreatingProcessID : 740
    Proc Time : 0
    CreatingProcessID : 916
    Proc Time : 6
    CreatingProcessID : 0
    Proc Time : 100

     

    Here is output from Windows 2003

    CreatingProcessID : 400
    Proc Time : 0
    CreatingProcessID : 932
    Proc Time : 0
    CreatingProcessID : 1020
    Proc Time : 0
    CreatingProcessID : 3168
    Proc Time : 0
    CreatingProcessID : 932
    Proc Time : 0
    CreatingProcessID : 0
    Proc Time : 0

     

    The C# code is as follows:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Management;
    using System.Threading;

    namespace Win32_ProcPerf
    {
        class Win32_ProcPerf
        {
            static void Main(string[] args)
            {
                System.Diagnostics.PerformanceCounter cpuCounter;

                if (args.Length < 1 || string.IsNullOrEmpty(args[0]))
                {
                    Console.WriteLine(
                      "Usage: processperf processname [loopvalue]");
                    return;
                }

                processName = args[0];
                int loopvalue = 1;

                if (args.Length == 2) //|| !string.IsNullOrEmpty(args[1]))
                {
                    try
                    {
                        loopvalue = Convert.ToInt16(args[1]);
                    }
                    catch (FormatException e)
                    {
                        Console.WriteLine("Optional Looping Paramater no numeric, defaulting to a loop value of 1");
                        loopvalue = 1;
                    }
                }


                for (int i = 0; i < loopvalue; i++)
                {

                    ManagementScope scope = new ManagementScope("\\\\.\\ROOT\\cimv2");

                    //create object query
                    ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_PerfFormattedData_PerfProc_Process");

                    //create object searcher
                    ManagementObjectSearcher searcher =
                                            new ManagementObjectSearcher(scope, query);

                    //get collection of WMI objects
                    ManagementObjectCollection queryCollection = searcher.Get();

                    //enumerate the collection.
                    foreach (ManagementObject m in queryCollection)
                    {
                        // access properties of the WMI object
                        Console.WriteLine("CreatingProcessID : {0}", m["CreatingProcessID"]);
                        Console.WriteLine("Proc Time : {0}", m["PercentProcessorTime"]);

                    }
                   
                }
            }
        }
    }

    Tuesday, June 7, 2011 6:25 PM