检测USB电源状态

发布于 2024-11-05 05:07:51 字数 297 浏览 0 评论 0原文

Windows 可以选择关闭某些外围设备(例如 USB 端口)以节省电量(可以通过设备管理器启用/禁用此行为)。断电发生在各种情况下,例如当笔记本电脑的盖子关闭时。这给我带来了一个问题,因为我有一个 GUI,它与连接到 USB 端口的硬件进行通信,并且每次关闭笔记本电脑盖子时通信都会被切断。有没有一种方法可以在这种断电(待机?)事件发生之前以编程方式检测到它,并更优雅地关闭我的 USB 设备?有没有办法以编程方式配置系统的每个 USB 端口以禁用此行为?

现在我正在查看 SystemEvents.PowerModeChanged,这是检测此事件的正确事件吗?

Windows has the option of powering down certain peripherals, such as USB ports, to save power (this behavior can be enabled/disabled via Device Manager). The power down happens under various conditions such as when the lid of a laptop is closed. This is causing a problem for me as I have a GUI which talks to hardware attached to the USB port and communications are severed every time the laptop lid is closed. Is there a way to programmatically detect this power-down (standby?) event before it happens and more gracefully shut down my USB device? Is there a way to programmatically configure each of the system’s USB ports to disable this behavior?

Right now I'm looking at SystemEvents.PowerModeChanged, is this the right event to detect this?

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

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

发布评论

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

评论(2

甲如呢乙后呢 2024-11-12 05:07:51

听起来您想要

  1. WM_POWERBROADCAST (http://msdn.microsoft.com/en-us/library/aa373247(v=vs.85).aspx)
  2. RegisterPowerSettingNotification ( http://msdn.microsoft.com/en-us/library/aa373196.aspx)

您首先需要调用RegisterPowerSettingNotification,然后您的应用程序将收到WM_POWERBROADCAST消息。

此页面有使用这些窗口消息的电源管理类的 c# 实现。 http://www.koders.com/csharp/fid00BAA34B0CAA3E320F9F5A44610A 015973BF28ED.aspx?s =nativemethods#L175

It sounds like you want

  1. WM_POWERBROADCAST (http://msdn.microsoft.com/en-us/library/aa373247(v=vs.85).aspx)
  2. RegisterPowerSettingNotification (http://msdn.microsoft.com/en-us/library/aa373196.aspx)

You first need to call RegisterPowerSettingNotification then WM_POWERBROADCAST messages will be received by your application.

This page has a c# implementation of a power management class using these window messages. http://www.koders.com/csharp/fid00BAA34B0CAA3E320F9F5A44610A015973BF28ED.aspx?s=nativemethods#L175

月亮是我掰弯的 2024-11-12 05:07:51

正如之前的海报所提到的,RegisterPowerSettingNotification 就是您想要的。为了澄清这一点,您可以从其他类型的 .NET 应用程序(控制台等)引用 Winforms (System.Windows.Forms.dll)。您可以通过子类化 Winform(Forms 类)并重写其 WndProc 来访问 Window 句柄(以便接收消息)。

MSDN 有一篇非常好的文章来实现这一点,并附有示例代码。

As mentioned by the previous posters RegisterPowerSettingNotification is what you want. To clarify, you can reference Winforms (System.Windows.Forms.dll) from other types of .NET applications (console, etc). You can get access to a Window handle (in order to receive messages) by subclassing a Winform (the Forms class) and overriding its WndProc.

MSDN has a very good article of doing just that, along with example code.

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