Disable sleep mode in Windows Mobile 6
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.
How can I detect suspend on Windows Mobile?
The quote (which sounds really, really familiar) is still true. The only components that are guaranteed to be able to complete their work before suspend are drivers, and they have a significant set of limitations as well.
The general idea behind a suspend is to be transparent to the application, and getting in front of that generally isn't a good idea.
How to stop a PDA sleeping
Have a look at:
http://www.pinvoke.net/default.aspx/coredll.SystemIdleTimerReset
Keep Windows Mobile 6 phone alive
As long as your app is doing something (in a loop or a Timer) it is relatively easy, you need:
public static class CoreTools
{
[DllImport("coredll.dll")]
public static extern void SystemIdleTimerReset();
}
And then call SystemIdleTimerReset()
regularly.
Disable Suspend in Window CE
Like in AAT's answer, you have to trigger reload event. Working implementation below:
private static void DoAutoResetEvent()
{
string eventString = "PowerManager/ReloadActivityTimeouts";
IntPtr newHandle = CreateEvent(IntPtr.Zero, false, false, eventString);
EventModify(newHandle, (int)EventFlags.EVENT_SET);
CloseHandle(newHandle);
}
private enum EventFlags
{
EVENT_PULSE = 1,
EVENT_RESET = 2,
EVENT_SET = 3
}
[DllImport("coredll.dll", SetLastError = true)]
private static extern IntPtr CreateEvent(IntPtr lpEventAttributes, bool bManualReset, bool bInitialState, string lpName);
[DllImport("coredll")]
static extern bool EventModify(IntPtr hEvent, int func);
[DllImport("coredll.dll", SetLastError = true)]
private static extern bool CloseHandle(IntPtr hObject);
Motorola MC65 - Windows Mobile 6.5 kills my app after resuming
Although Windows Mobile may kill applications if the resources getting low and the application does not react on WM_HIBERNATE messages, this will not be the cause for your current setup.
An application will disappear from screen if it is minimized/hidden. This may also happen, if another app comes to foreground and is closed later. The window stack is then changed and your app is not in foreground any more.
An app may crash on a suspend/resume cycle if it accesses resources that will be suspended when the device enters suspend state. These resources may be network connections or volume (storage) resources. What is being suspended during a suspend depends on the Power Management settings. A network may be suspended to save power or a mounted volume (storage card) may be unloaded (although this should not be the case with actual devices).
So, either your app is using a network connection that is suspended and your app is not robust against network changes or, as stated in the notes, the device does unload the storage card driver during suspend/resume. For the later either move your app to the device storage or contact the vendor for a another power management profile, where external storage is maintained during suspend/resume.
How do I keep a Windows Mobile Professional Device in the Unattended Power State
I recommend that you don't disable the sleep mode, but instead register events to wake up your device and run your task. Have a look at the Smart Device Framework at OpenNetCf. Among others there you will find:
- Methods for registering wake up events
- Event handlers for Power Down events
Most of them are in the OpenNETCF.WindowsCE namespace.
How to continue execution when mobile device sleeps?
I assume you mean that you want your code to continue executing after the device suspends? First off, you can't. When the device suspends, the processor stops running. You have a couple options though. You can periodically call SystemIdleTimerReset to prevent the device from suspending, run it in "unattended mode" so the backlight shuts off but the device does not suspend, or use an API like CeRunAppAtTime to preiodically wake the processor to run your code.
How do I prevent an Android device from going to sleep programmatically?
One option is to use a wake lock. Example from the docs:
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
wl.acquire();
// screen and CPU will stay awake during this section
wl.release();
There's also a table on this page that describes the different kinds of wakelocks.
Be aware that some caution needs to be taken when using wake locks. Ensure that you always release()
the lock when you're done with it (or not in the foreground). Otherwise your app can potentially cause some serious battery drain and CPU usage.
The documentation also contains a useful page that describes different approaches to keeping a device awake, and when you might choose to use one. If "prevent device from going to sleep" only refers to the screen (and not keeping the CPU active) then a wake lock is probably more than you need.
You also need to be sure you have the WAKE_LOCK permission set in your manifest in order to use this method.
Related Topics
#Error Please Use the /Md Switch for _Afxdll Builds
Any Good Solutions for C++ String Code Point and Code Unit
With C++, I Get Pointer with 0Xcdcdcdcd When Creating a Class - What Is Happening
Why Does Makeintresource() Work
How to Embed the Gnu Octave in C/C++ Program
C++ Class Member Function Callback
How to Ignore False Positive Memory Leaks from _Crtdumpmemoryleaks
Opencv Gtk+2.X Error - "Unspecified Error (The Function Is Not Implemented...)"
How Concatenate a String and a Const Char
Why Does 'Std::Basic_Ifstream<Char16_T>' Not Work in C++11
Set the Digits After Decimal Point
Understanding Lapack Calls in C++ with a Simple Example
Reading Directly from an Std::Istream into an Std::String
What Is the Meaning of 'Struct X Typedef' VS. 'Typedef Struct X'
How to Easily Indent Output to Ofstream
What Is the Cin Analougus of Scanf Formatted Input
Undefined Symbols for Architecture X86_64: Which Architecture Should I Use