每个程序的CPU使用率 [英] CPU Usage per program
本文介绍了每个程序的CPU使用率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我被要求构建类似任务管理器的东西,我需要获取进程列表以及它们使用的%% cpu值.
这些不需要运行计数,我只需要我的应用程序启动时正在使用的CPU百分比即可.
我正在使用以下代码获取cpu百分比
I have been asked to build something like the task manager, I need to get a list of processes and how much of the cpu they are using in % value.
These dont need to be running counts i just need what percentage of the cpu was being used when my application started up.
I am getting the cpu percentage using the below code
Public Shared Function GetCpuUsage() As Integer
Dim myCount As Integer
Using cpu = New PerformanceCounter()
With cpu
.CategoryName = "Processor"
.CounterName = "% Processor Time"
.InstanceName = "_Total"
End With
myCount = cpu.NextValue()
System.Threading.Thread.Sleep(1000)
myCount = cpu.NextValue()
System.Threading.Thread.Sleep(1000)
myCount = cpu.NextValue()
End Using
Return myCount
End Function
我有一个问题是如何获取使用cpu用法的每个进程的%值,而忽略0处的任何值?
The question I have is how would i get the % value of each process that is using any cpu usage and ignore anything that is at 0?
推荐答案
看看:性能计数器 [使用PDH功能消耗计数器数据 [ System.Diagnostics.PerformanceData命名空间 [这篇文章 [
Have a look at: Performance Counters[^], Using the PDH Functions to Consume Counter Data[^] and System.Diagnostics.PerformanceData Namespace[^]
I guess this article[^] will help too.
Best regards
Espen Harlinn
出于这种目的,您可以使用WMI或System.Management
,
http://msdn.microsoft.com/en-us/library/system.management.aspx [^ ].
这是一篇简短的入门文章:
http://www.csharphelp.com/2006/10/wmi-made-easy -for-c/ [ ^ ].
这是查询CPU使用率所需要的:
http://msdn.microsoft.com/zh-我们/library/windows/desktop/aa394277%28v=vs.85%29.aspx [ http://bit.ly/QMh4rI [ ^ ].
只是一个相关的代码示例:
http://social.msdn.microsoft.com /Forums/zh-CN/csharplanguage/thread/469ec6b7-4727-4773-9dc7-6e3de40e87b8/ [ http://social.msdn.microsoft.com /Forums/zh-CN/csharplanguage/thread/469ec6b7-4727-4773-9dc7-6e3de40e87b8/ [
For such purposes, you can use WMI, orSystem.Management
,
http://msdn.microsoft.com/en-us/library/system.management.aspx[^].
This is a nice and short introductory article:
http://www.csharphelp.com/2006/10/wmi-made-easy-for-c/[^].
This is what you will need to query CPU usage:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa394277%28v=vs.85%29.aspx[^].
There is a number of code samples with some cookbook recipes for WMI query which you can easily find if you Google; for example:
http://bit.ly/QMh4rI[^].
Just one relevant code sample:
http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/469ec6b7-4727-4773-9dc7-6e3de40e87b8/[^].
And this is the CodeProject article on the topic:
http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/469ec6b7-4727-4773-9dc7-6e3de40e87b8/[^].
I hope it''s enough for you to solve your problem.
Good luck,
—SA
这篇关于每个程序的CPU使用率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文