tags:

views:

577

answers:

3

Hi, How can I prevent the pocket PC device from shutting down from my application when the power button pressed? I am using C#.

A: 

You can try changing the power requirements for the device "BLK1:", which is the blacklight device. Be aware that the behavior may not be the same on all devices and version of the OS or Vendor specific Extensions.

To do this, you can write something like :

    [DllImport("coredll")]
    private extern static IntPtr SetPowerRequirement(string pvDevice, int deviceState, 
                int deviceFlags, IntPtr pvSystemState, int stateFlags);

    [DllImport("coredll")]
    private extern static int ReleasePowerRequirement(IntPtr handle);

and call it this way :

    IntPtr handle = SetPowerRequirement("BLK1:", 0 /* D0, Full On */, 1, IntPtr.Zero, 0);
    // Do something that requires the device to stay on ...
    ReleasePowerRequirement(handle);

But this is generally not a good practice, leaving a device with the backlight on for extended periods might reduce dramatically its autonomy.

Jerome Laban
+1  A: 

You could use the Microsoft.WindowsCE.Form.MessageWindows class to intercept the Power Button event. This solution will not be portable, as the hardware key will be different in different machines.

I recommend however that you don't disable power down completely. Have a look at my answer in another question here. You could also use openetcf to easily create power down events handlers and register wake up events. You should implement the application logic based on what you are trying to achieve, for instance wake up every one minute to run a process.

kgiannakakis
A: 

Thank you so much for the answer. I already used the registry key to intract with all the keys however the only key I am not able to intract with is power key. Could you please let me know what is the value for powerkey? My application runs all time and the appliction will turn on/off the device in certain time however when the device turn off by power key the application wont be able to turn on it again. So if somehow I be able to control the power key it will solve the problem. Thanks, Tammhy