像在任务管理器中那样获取每个进程的进程CPU使用率

时间:2014-05-23 07:22:46

标签: c# cpu-usage performancecounter

下面是获取每个进程CPU使用率的代码,但它似乎为所有进程提供0而不匹配任务管理器CPU列数据。

 Process[] myProcesses = Process.GetProcesses();
for (int i = 0; i < myProcesses.Length; i++)
    {
        //Get process name by processId
        appName = GetProcessInstanceName(myProcesses[i].Id);

        //CPU Column value             
        PerformanceCounter process_cpu = new PerformanceCounter("Process", "% Processor Time", appName,true);                
        Int32 process_pct = Convert.ToInt32(process_cpu.NextValue());                
        System.Threading.Thread.Sleep(1000);

        Int32 process_pct_Second = Convert.ToInt32(process_cpu.NextValue());
        process_pct_Second = process_pct_Second / Environment.ProcessorCount;   
    } 

如果我在这里遗失某些东西,请纠正我。

0 个答案:

没有答案