65.9K
CodeProject is changing. Read more.
Home

Keep PDA Awake using C#

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.30/5 (4 votes)

Jun 4, 2007

CPOL
viewsIcon

30610

Keep PDA awake using C#

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)
            {
                // TODO
            }
        }
        /**/
        /// <summary>
        /// </summary>
        public static void DisableDeviceSleep()
        {
            nDisableSleepCalls++;
            if (nDisableSleepCalls == 1)
            {
                //Debug.Assert(preventSleepTimer == null);
                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