识别 Windows 计算机上的空闲状态

发布于 2024-08-10 19:21:49 字数 247 浏览 2 评论 0原文

我知道 GetLastInputInfo 方法,但这只会给我自上次用户输入(键盘或鼠标)以来的持续时间。如果最后一次收到用户输入是在 10 分钟前,这并不意味着系统已经空闲了 10 分钟 - 扫描、下载、电影 - 有很多原因。

那么我们如何判断系统是否真正空闲呢?

有谁知道Windows对“空闲”的定义是什么? 必须是阈值的组合 - 例如低于 5% 的 cpu 利用率、低于 3% 的磁盘利用率等 - 以及用户输入参数...有人知道确切的定义吗?

I know about the GetLastInputInfo method but that would only give me the duration since last user input - keyboard or mouse. If a user input was last received 10 minutes ago, that wouldn't mean the system has been idle for 10 minutes - scans, downloads, movies - lots of reasons.

So how can we identify whether the system is truly idle or not?

Does anyone know what Window's definition of "idle" is?
Must be a combination of thresholds - like less than 5% cpu util, less than 3% disk util etc. - along with the user input parameter...anyone knows the exact definition?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

固执像三岁 2024-08-17 19:21:49

由于大多数 Windows 应用程序都是 UI 驱动的,并且 Windows UI 是通过从消息队列中获取消息来驱动的,因此定义应用程序何时“空闲”的一种常见方法是当其消息队列中没有消息时。

Windows 应用程序中总是有消息(用户生成的和系统生成的),但您会在典型的应用程序中看到频繁的短暂空闲期。当鼠标在应用程序中的窗口上移动时,您的空闲时间将降至接近零,直到鼠标停止移动,因为鼠标活动会生成大量消息活动。

因此,在 Windows 应用程序的消息循环中,如果您当前正在使用 GetMessage,则需要更改循环以使用 PeekMessage。当消息队列为空时,GetMessage 会阻塞,直到有消息到达。如果队列中有消息,PeekMessage 返回 true;如果没有消息,则返回 false。使用标志,您可以使 PeekMessage 在同一调用中获取消息。

当 PeekMessage 返回 false 时,您可以将执行转移到“空闲”任务,而不是等待下一条消息。

在 WinForms 应用程序中,使用 Application.Idle 事件

不过,不要在空闲代码中执行太多操作,否则您的 UI 会因为消息处理不快而感觉迟缓。过度使用空闲周期还会使您的 CPU 运行过热并耗尽笔记本电脑的电池。

Since most Windows apps are UI driven, and Windows UI is driven by fetching messages from the message queue, one common way to define when an application is "idle" is when it has no messages in its message queue.

There are always messages flying around in a Windows app (user generated and system generated), but you will see frequent short idle periods in a typical app. When the mouse moves over a window in your app, your idle periods will drop to near zero until the mouse stops moving because mouse activity generates a lot of message activity.

So, in your Windows app's message loop, if you are currently using GetMessage you will need to change your loop to use PeekMessage instead. GetMessage blocks when the message queue is empty until a message arrives. PeekMessage returns true if there is a message in the queue or false if there is none. With flags you can make PeekMessage get the message in the same call.

When PeekMessage returns false, you can divert execution to your "idle" task instead of waiting for the next message.

In WinForms applications, use the Application.Idle event

Don't do too much in your idle code though, or your UI will feel sluggish because messages aren't being processed quickly. Overuse of the idle cycle will also make your CPU run hot and drain your laptop battery.

两相知 2024-08-17 19:21:49

“空闲”没有真正的定义。它会是你想要的任何东西。

There is no true definition of "idle". It would be whatever you would want it to be.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文