积极答复者
win7下如何使用wmi获取指定进程的CPU使用率?

问题
答案
-
Hi 终于出名,
如果你计算线程或者它的子线程占用的cpu使用率,你可以参考这个代码:
How to get CPU usage of processes and threads
http://www.codeproject.com/Articles/10258/How-to-get-CPU-usage-of-processes-and-threads
这个sample用的是NtQuerySystemInformation 非官方文档的api来计算使用率:
use remaining time = user Time - last user Time;
kernal remaining time = kernel Time - last kernel Time;
idl remaining time = idle Time - last idle Time;系统时间就是kernal剩余时间加上用户时间:
sys time = kernal remaining time + use remaing time
最后cpu时间就是:
cpu = int( (sys - idl) *100 / sys );
你可以参考http://www.codeproject.com/Articles/9113/Get-CPU-Usage-with-GetSystemTimes来知道如何用GetSystemTimes来获得CPU使用率。
第二种方法你可以使用PerformanceCounter 来实现,这个方法你可以看Microsoft 上发布的Track the system CPU usage (CSCpuUsage).
http://code.msdn.microsoft.com/CSCpuUsage-f009d9c1
orichisonic http://blog.csdn.net/orichisonic If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
- 已标记为答案 Vicky SongMicrosoft employee, Moderator 2012年3月17日 1:42
全部回复
-
Hi 终于出名,
如果你计算线程或者它的子线程占用的cpu使用率,你可以参考这个代码:
How to get CPU usage of processes and threads
http://www.codeproject.com/Articles/10258/How-to-get-CPU-usage-of-processes-and-threads
这个sample用的是NtQuerySystemInformation 非官方文档的api来计算使用率:
use remaining time = user Time - last user Time;
kernal remaining time = kernel Time - last kernel Time;
idl remaining time = idle Time - last idle Time;系统时间就是kernal剩余时间加上用户时间:
sys time = kernal remaining time + use remaing time
最后cpu时间就是:
cpu = int( (sys - idl) *100 / sys );
你可以参考http://www.codeproject.com/Articles/9113/Get-CPU-Usage-with-GetSystemTimes来知道如何用GetSystemTimes来获得CPU使用率。
第二种方法你可以使用PerformanceCounter 来实现,这个方法你可以看Microsoft 上发布的Track the system CPU usage (CSCpuUsage).
http://code.msdn.microsoft.com/CSCpuUsage-f009d9c1
orichisonic http://blog.csdn.net/orichisonic If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
- 已标记为答案 Vicky SongMicrosoft employee, Moderator 2012年3月17日 1:42