Get the Cpu usage of each process from wmi

一个人想着一个人 提交于 2019-11-30 20:14:00

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:-

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!