views:

385

answers:

1

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.

+5  A: 

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
+1 for no good application should (be allowed) to ever do this. When a user power's down a Windows Mobile device, its typically because they want to preserve any power they have left. Even desktop windows now ignores applications attempts to prolong shutdown: I think on Windows Mobile you will find even less OS support for ignoring the users shutdown.
Chris Becke