views:

100

answers:

1

I have a windows mobile 5 program (compact framework 3.5) that I need to be able to detect when the device is idle.

Right now I am just checking to see if the backlight is off. Like this:

[DllImport("coredll.dll", EntryPoint = "sleep", SetLastError = true)]
internal static extern void sleep(int dwMilliseconds);

....

//Get the current power state of the system
int winError = CoreDLL.GetSystemPowerState(systemStateName, out systemPowerStates);
if (winError == 0)
{
    //If the backlight is off, consider the state to be idle.
    if (systemStateName.ToString() == "backlightoff")
    {
        idle = true;
    }
}

I think this may be getting close, but I would like to know if the device is truly not being used.

+1  A: 

You're using the right function, simply check for the states (which are bitwise flags):

if ((systemPowerStates & POWER_STATE_IDLE) == POWER_STATE_IDLE) {
  idle = true;
}

with POWER_STATE_IDLE = 0x00100000.

Edit: to answer your comment, look at the RequestPowerNotification function. You'll receive POWER_BROADCAST message when the power state changes.

Julien Lebosquain
That worked good. (It did not solve my problem, but it worked.) I think I am going to have to go for POWER_STATE_USERIDLE (0x01000000). However, after a long idle period, I am getting 0x02000000 which is not defined in pm.h.
Vaccano
I am still stuck on this. I used the method you indicated, but POWER_STATE_IDLE is never set. Is that because we are using a timer to keep checking to see if the power state is idle? Does that prevent it from going to idle state? If so, how can you do this if checking causes it to not be idle? (Once you check you loose the idle state.)
Vaccano
Sweet! Thanks. I will try that out.
Vaccano
Just as an fyi to future searchers, The RequestPowerNotifications did fire when the device went idle, but it did not have POWER_STATE_IDLE in the "parameter". (It only had POWER_STATE_ON and POWER_STATE_PASSWORD.) I ended up changing a approaches and going with Chris Tacke's excellent solution for finding an idle application: http://blog.opennetcf.com/ctacke/2009/05/19/DetectingApplicationIdle.aspx
Vaccano