获取总CPU使用的百分比
我正在尝试将总CPU的百分比用于 label1.caption
我已经搜索并发现这些:
-
不起作用 - http://www.vbforums.com/showthread.php?345723-delphi-delphi-get-get-cpu-usage
-
不是我需要的 - http://delphi.cjcsoft.net/viewthread.php?tid=42837
这是我正在努力的:
我相信有一种简单的方式,例如我们获得RAM使用时。
GlobalMemoryStatus(RamStats);
Label1.Caption := Format('RAM: %d %%', [RamStats.dwMemoryLoad]);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我找到了一篇文章,确定有关如何获取当前过程的CPU使用情况的cpu-usage-process-corcess-c-and-c 。
现在,我们需要做更多的事情来计算每个运行过程的CPU使用百分比来计算总CPU使用率:
在获得运行过程ID之后,我们开始致电
deletenonexistingProcessIDSFROMCACHE
清理缓存,该缓存保留了以前所需的CPU使用时间getProcesscpuusagepct
:自上次查询以来已停止的每个过程都从此缓存中删除。getProcesscpuusagepct
是核心-c-and-c/“ rel =“ noreferrer”>确定cpu-usage of-rurrent-process-c-and-c and-c 。此功能需要从CPU用法中检索先前的读数CACHE最新的Processcpuusagecache
(单元中的全局)使用ProcessID。请注意,不建议拨打
getToAlcpuusagecpu
小于每200毫秒,因为它可能给出错误的结果。这是Windows 7上的结果的屏幕截图。
I have found an article, determine-cpu-usage-of-current-process-c-and-c, about how to get the CPU usage of the current process.
Now we need to do a bit more to compute the Total CPU usage percentage by adding up CPU usage percentage for each running processes:
After getting running process id's, we start out calling
DeleteNonExistingProcessIDsFromCache
to clean up the cache, that holds previous Cpu usage times needed inGetProcessCpuUsagePct
: Every process that has been stopped since last query is removed from this cache.The
GetProcessCpuUsagePct
is the core, which is a translation of determine-cpu-usage-of-current-process-c-and-c. This function needs to retrieve the previous reading from the Cpu Usage CacheLatestProcessCpuUsageCache
(global in the unit) using the ProcessID.Note, it is not recommended to call
GetToalCpuUsageCpu
less than every 200 ms, as it may give wrong results.Here is a screen shot of the result on a Windows 7.
Full Listing of unit:
Test Code:
unit Unit1;
您可以使用性能计数器功能来自Microsoft。
我发现 /a> - 参见 cpu当前使用 - 来自 lanzelot> lanzelot 在这里用户对Delphi进行了一些移植。
RAW PORTING :
示例需要
pdh
我从控制台应用程序中的基本测试:
一个基于
tthread
class 的更有用的示例。这允许根据构造函数中传递给
acounterpath参数的参数获得不同的计数器。
counterthread.pas
unit1.pas
unit1.dfm
主题
我目前在家,我在这里没有Delphi XE,所以我用Turbo Delphi编码了它,我的机器上没有安装
pdh
单元,目前我不知道是否delphi XE有单位。注意
我已经使用了函数而不是 pdhaddenglishcounter 因为单元中缺少函数参考。不幸的是,在添加了引用后,我旧的Windows XP上的
pdh.dll
中仍缺少该功能。pdhaddcounter
的\ processore(_total)\%tempo processore
上使用意大利局部化路径。如果使用
pdhaddenglishCounter
函数或您的语言环境为英语,则必须使用路径\ processor(_total)\%处理器时间
。如果您的系统语言环境除英语或意大利语外,则必须使用 pdhbrowsecounters 函数。
以下的非常基本的功能用法需要以获取进一步的参考。
You can achieve your goal using the Performance Counters Functions from Microsoft.
I have found this answer - see CPU currently used - from the Lanzelot user here on SO and I have done some porting to Delphi.
Raw porting:
The example requires the
pdh
unit which I have grabbed from here.The
WinPerf
unit is needed by thepdh
and I have downloaded it from here.Basic test in a console application:
A more useful example based on the
TThread
class.This allows to obtain different counters based on the parameter passed to the
ACounterPath
argument in the constructor.counterThread.pas
Unit1.pas
Unit1.dfm
OFF TOPIC
I'm currently at home and I've not a Delphi XE here so I coded it with Turbo Delphi, I have no
pdh
unit installed on my machine and I can't know at the moment if Delphi XE has the units.NOTICE
I have used the PdhAddCounter function instead of the PdhAddEnglishCounter because the function reference is missing in the unit. Unfortunately, after I added the reference, the function was still missing in the
Pdh.dll
on my old Windows XP.The
szFullCounterPath
of thePdhAddCounter
is localized so I have to use the italian localized path on my Windows\Processore(_Total)\% Tempo Processore
.If you use the
PdhAddEnglishCounter
function or your locale is english, you have to use the path\Processor(_Total)\% Processor Time
.If your system locale is other than english or italian, you have to find the path by yourself using the PdhBrowseCounters function.
The very basic function usage which follows needs the
PdhMsg
unit.See also MSDN Browsing Performance Counters for further reference.
http://www.magsys.co.uk/delphi/
获得magwmi组件。它是免费的。
此组件将允许您轻松访问WMI,该WMI已经具有所需的信息。我刚刚测试了我在Win 10上使用此元素的旧程序,它正确地找到了我的所有8个内核和处理器的使用情况。
然后做这样的事情:
http://www.magsys.co.uk/delphi/
Get the MagWMI component. It's free.
This component will allow you to access the WMI pretty easily which already has the info you want. I just tested an old program I had using this on Win 10 and it correctly found all 8 of my cores and the processor usage.
And then do something like this:
我这样解决:
这样,您可以从 wmic 中获得任何参数值
I solve this way:
In this way, you can get any param values from wmic
我发现 thi s
工作
对我有用
I found t h i s
does the job
worked for me
