检测USB电源状态
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来您想要
您首先需要调用RegisterPowerSettingNotification,然后您的应用程序将收到WM_POWERBROADCAST消息。
此页面有使用这些窗口消息的电源管理类的 c# 实现。 http://www.koders.com/csharp/fid00BAA34B0CAA3E320F9F5A44610A 015973BF28ED.aspx?s =nativemethods#L175
It sounds like you want
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
正如之前的海报所提到的,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.