检测并防止/延迟Windows移动电源关闭
我有一个窗口移动应用程序,需要在设备关闭之前检测电源关闭并执行一些操作。
我已经能够使用 RequestPowerNotifications win32 api 的 pinvoke 检测到设备正在关闭。
但是我不知道如何长时间关闭电源来运行我的代码。
I have a window mobile application which needs to detect power off and perform some operation before the device shuts down.
I have been able to detect that the device is shutting down using pinvoke to the RequestPowerNotifications win32 api.
However I can not figure out how to block the power off for long enough to run my code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
作为一个应用程序,您无法阻止它,除非 OEM 以某种方式提供了一个自定义 API 来执行此操作(我从未在任何 WInMo 设备上见过它,只在一些自定义 CE 设备上见过它)。当电源管理器被告知暂停时,它会发出通知并立即开始关闭。唯一有机会延迟事情的子系统是驱动程序,并且它们在此期间无法调用 Win32 API(这意味着有机会关闭外设电源、保存寄存器等)。
我也不能说我见过任何应用程序这样做的充分理由。如果用户按下电源按钮,他们希望关闭电源,而不是被应用程序阻止。如果操作系统需要关闭电源(例如由于电量低),那么阻止它无论如何都是一件坏事。
As an application, you can't prevent it unless the OEM has somehow provided a custom API for doing so (and I've never seen it on any WInMo device, only on a few custom CE devices). Whent he power manager is told to suspend, it sends out the notification and begins shutdown immediately. The only subsystems that get an opportunity to delay things are drivers, and they can't call Win32 APIs during that time (it's meant as an opportunity to power off peripherals, save registers, etc).
I also can't say I've ever seen a good reason for an application to ever do this. If a user pushes the power button, they want to power down - not be inhibited by an app. If the OS needs to power down (like due to low power) then preventing it would be a bad thing anyway.
正如 ctacke 指出的:这很脏。但是,只要电源通知线程指示设备正在尝试挂起,您就可以尝试
SetSystemPowerState( POWER_STATE_ON );
。http://msdn.microsoft.com/en-us/library/ms920754.aspx
-PaulH
As ctacke pointed out: this is dirty. But, you could try
SetSystemPowerState( POWER_STATE_ON );
whenever your power notification thread indicates the device is trying to suspend.http://msdn.microsoft.com/en-us/library/ms920754.aspx
-PaulH