Detect and prevent/delay windows mobile power off

2019-07-16 06:41发布

问题:

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.

回答1:

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.



回答2:

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