Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Keep PDA Awake using C#

0.00/5 (No votes)
3 Jun 2007 1  
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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here