Introduction
In some projects, we run an application on PDA or pocket PC. After a few minutes, the mobile will go to sleep. We should turn on the device again. So I tried to find out a way to solve the issue and keep PDA awake.
Using the Code
using System.Runtime.InteropServices;
using Microsoft.Win32;
[DllImport("CoreDll.dll")]
private static extern void SystemIdleTimerReset();
private static int nDisableSleepCalls = 0;
private static System.Threading.Timer preventSleepTimer = null;
private static void PokeDeviceToKeepAwake(object extra)
{
try
{
SystemIdleTimerReset();
}
catch (Exception e)
{
}
}
public static void DisableDeviceSleep()
{
nDisableSleepCalls++;
if (nDisableSleepCalls == 1)
{
preventSleepTimer = new System.Threading.Timer
(new System.Threading.TimerCallback(PokeDeviceToKeepAwake),
null, 0, 30 * 1000);
}
}
private void FrmMain_Load(object sender, EventArgs e)
{
DisableDeviceSleep();
}
History
- 4th June, 2007: Initial post