Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable sleep mode in Windows Mobile 6

Does anyone know how could I programatically disable/enable sleep mode on Windows Mobile?

Thanks!

like image 634
Niko Gamulin Avatar asked Oct 29 '08 10:10

Niko Gamulin


1 Answers

If you want your program to not be put to sleep while it's running, the best way is to create a KeepAlive type function that calls SystemIdleTimerReset, SHIdleTimerReset and simulates a key touch. Then you need to call it a lot, basically everywhere.

#include <windows.h>
#include <commctrl.h>

extern "C"
{
    void WINAPI SHIdleTimerReset();
};

void KeepAlive()
{
    static DWORD LastCallTime = 0;
    DWORD TickCount = GetTickCount();
    if ((TickCount - LastCallTime) > 1000 || TickCount < LastCallTime) // watch for wraparound
    {
        SystemIdleTimerReset();
        SHIdleTimerReset();
        keybd_event(VK_LBUTTON, 0, KEYEVENTF_SILENT, 0);
        keybd_event(VK_LBUTTON, 0, KEYEVENTF_KEYUP | KEYEVENTF_SILENT, 0);
        LastCallTime = TickCount;
    }
}

This method only works when the user starts the application manually.

If your application is started by a notification (i.e. while the device is suspended), then you need to do more or else your application will be suspended after a very short period of time until the user powers the device out of suspended mode. To handle this you need to put the device into unattended power mode.

if(!::PowerPolicyNotify (PPN_UNATTENDEDMODE, TRUE))
{
    // handle error
}

// do long running process

if(!::PowerPolicyNotify (PPN_UNATTENDEDMODE, FALSE))
{
    // handle error
}

During unattended mode use, you still need to call the KeepAlive a lot, you can use a separate thread that sleeps for x milliseconds and calls the keep alive funcation.

Please note that unattended mode does not bring it out of sleep mode, it puts the device in a weird half-awake state.

So if you start a unattended mode while the device in suspended mode, it will not wake up the screen or anything. All unattended mode does is stop WM from suspending your application. Also the other problem is that it does not work on all devices, some devices power management is not very good and it will suspend you anyway no matter what you do.

like image 127
Shane Powell Avatar answered Sep 22 '22 01:09

Shane Powell