Get the Cpu usage of each process from wmi

前端 未结 1 1439
刺人心
刺人心 2021-01-02 15:30

I found many sources to get the cpu usage of each process. in general there are many ways to get the cpu usage of process .

  1. percentprocessortime from win32_per
相关标签:
1条回答
  • 2021-01-02 16:13

    You can use WMI to query this. I think you are looking for Win32_PerfFormattedData_PerfProc_Process class.

    using System;
    using System.Management;
    using System.Windows.Forms;
    
    namespace WMISample
    {
      public class MyWMIQuery
      {
        public static void Main()
        {
            try
            {
                ManagementObjectSearcher searcher = 
                    new ManagementObjectSearcher("root\\CIMV2", 
                    "SELECT * FROM Win32_PerfFormattedData_PerfProc_Process"); 
    
                foreach (ManagementObject queryObj in searcher.Get())
                {
                    Console.WriteLine("-----------------------------------");
                    Console.WriteLine("Name: {0}", queryObj["Name"]);
                    Console.WriteLine("PercentProcessorTime: {0}", queryObj["PercentProcessorTime"]);
                }
            }
            catch (ManagementException e)
            {
                MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
            }
        }
      }
    }
    

    Output:- Output when I run on my machine

    0 讨论(0)
提交回复
热议问题