计算CPU使用率Windows进程?使用率、进程、CPU、Windows

2023-09-04 02:30:27 作者:万世浮华

给定一个进程ID,我怎样才能从这个过程中的CPU负载用C#?

Given a process ID, how can i get the cpu load from this process with C#?

我一直使用的PerformanceCounter在http://stackoverflow.com/questions/1277556/c-calculate-cpu-usage-for-a-specific-application

I have tried using PerformanceCounter as described in http://stackoverflow.com/questions/1277556/c-calculate-cpu-usage-for-a-specific-application

我也尝试手动采取的总CPU时间两个值过程中的时间框架内,并划分增量CPU时间与增量的总时间。

I have also tried to manually take two values of the total cpu time for the process within a timeframe and divide the delta-cpu-time with the delta-total-time.

这两种方法似乎很好地工作在第一,但如果我把我的应用程序旁边的Windows任务管理器我的应用程序总是显示约两倍的任务管理器。我曾尝试与不同的时间步长更新,从10毫秒到10秒,并得到相同的结果,所有的时间。

Both these methods seems to work fine at first but if i put my application next to windows task manager my application always shows about twice as much as the task manager does. I have tried to update with various timesteps from 10ms to 10sec and get same result all the time.

嗯,意识到当我写这一点,它可能是某种形式的双核心问题......

Hmm, realized when i wrote this that it might be some kind of dual core issue...

推荐答案

如果你有两个内核,任务管理器会报告1 100%的CPU使用率高达50%。这可能是你的问题就在这里。

If you have two cores, the task manager is going to report 100% usage of 1 CPU as 50%. That's probably your issue right there.