处理器使用 [英] Processor usage

查看:58
本文介绍了处理器使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
使用 WMI 的 CPU 使用率 &C#

如何使用 WMI 在 C# (.NET) 中检索 CPU 使用率百分比?是的,没有任何愚蠢的 PerformanceCounter.

How to retrieve CPU usage % in C# (.NET) using WMI? Yes, without any stupid PerformanceCounter.

推荐答案

PerformanceCounter 是一种非常 成本极低的信息检索方式.与 WMI 不同,WMI 将为您提供完全相同的信息,但由于它运行在 COM 之上,因此开销很大.

PerformanceCounter is a very low cost way of retrieving this info. As opposed to WMI which will give you the exact same info but with lots of overhead because it runs on top of COM.

它和 PC 有完全相同的问题,这就是为什么我猜你认为它是愚蠢的".CPU 使用百分比是在一个时间间隔内测量的.实际 CPU 使用率是 0 或 100%,CPU 永远不会故意以较低的速率运行.当有一个线程有工作要做时,它会全速运行代码.如果没有工作要做,您的台式机正常状态,则使用 HLT 指令关闭 CPU.再次被时钟中断唤醒.

And it has the exact same problem as PC, which is why I'd guess you think it is 'stupid'. CPU usage percent is measured over an interval. Actual CPU usage is either 0 or 100%, the CPU never runs intentionally at a lower rate. It runs code at full bore when there's a thread that has work to do. If there is no work to do, the normal state of your desktop machine, then the CPU is powered off with the HLT instruction. It is woken up again with the clock interrupt.

TaskMgr 或 Permon 中显示的 CPU 使用率百分比是超过一秒的计算值.它以 100% 运行的时间量与关闭的时间量之比.间隔长度非常很重要.你做的越短,计算出的值就越跳跃".连续调用 NextValue() 两次,您将始终获得 100%.

The CPU usage percentage as shown in TaskMgr or Permon is a calculated value over one second. The ratio of the amount of time it was running at 100% vs the amount of time it was turned off. The interval length is very important. The shorter you make it, the 'jumpier' the calculated value gets. Down to calling NextValue() twice in a row, you'll always get 100%.

长话短说:你需要一个计时器.花一秒钟时间模拟您在 PerfMon 和 TaskMgr 中看到的内容.

Long story short: you need a timer. Make it one second to emulate what you see in PerfMon and TaskMgr.

这篇关于处理器使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆