Click here to Skip to main content
15,897,226 members
Articles / Programming Languages / C#

Windows Mobile Programming Tricks on the .NET Compact Framework: Part 1

Rate me:
Please Sign up or sign in to vote.
4.64/5 (8 votes)
23 Feb 2011CPOL4 min read 41.1K   668   24  
The article provides and describes some useful code snippets for Windows Mobile/CE developers.
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace BeeMobile
{
    /// <summary>
    /// Represents a functionality which periodically resets the idle timers which measure how
    /// long the user has been idle and subsequently turn the display off.
    /// </summary>
    /// <remarks>
    /// This is useful when writing applications which require the display to be constantly lit.
    /// </remarks>
    public class ResetIdleTimer
    {
#region WIN32
        [DllImport("coredll.dll")]
        private static extern void SystemIdleTimerReset();

        [DllImport("Aygshell.dll")]
        private static extern void SHIdleTimerReset();

        [DllImport("coredll.dll")]
        private static extern int SetSystemPowerState(string pwrState, int pwrStateFlags, int options);

        private const int POWER_STATE_ON = 0x10000;
        private const int POWER_STATE_OFF = 0x20000;
        private const int POWER_STATE_SUSPEND = 0x200000;
        private const int POWER_FORCE = 4096;
#endregion WIN32

        #region " Data members "

        private Timer m_Timer;

        #endregion " Data members "



        #region Constructor
        /// <summary>
        /// Constructs a new ResetIdleTimer object with default 500 ms timer refresh interval.
        /// </summary>
        public ResetIdleTimer()
            : this(500)
        {
        }

        /// <summary>
        /// Constructs a new ResetIdleTimer object.
        /// </summary>
        /// <param name="milisec">Specifies how often the timers should be reset.</param>
        public ResetIdleTimer(int milisec)
        {
            m_Timer = new Timer();
            m_Timer.Interval = milisec; // in milliseconds
            m_Timer.Tick += new EventHandler(Timer_Tick);
        }
        #endregion constructor

        #region Public methods

        /// <summary>
        /// Turns on the display and resets the idle timers.
        /// </summary>
        public static void ResetTimer()
        {
            SetSystemPowerState(null, POWER_STATE_ON, POWER_FORCE);
            SystemIdleTimerReset();
            SHIdleTimerReset();
        }

        /// <summary>
        /// Turns the backlight off.
        /// </summary>
        public static void TurnOffBackLight()
        {
            SetSystemPowerState(null, POWER_STATE_OFF, POWER_FORCE);
        }

        /// <summary>
        /// Call this method to keep the display lit.
        /// </summary>
        public void StartTimer()
        {
            m_Timer.Enabled = true;
        }

        /// <summary>
        /// Call this method to turn off the functionality provided by this class.
        /// </summary>
        public void StopTimer()
        {
            m_Timer.Enabled = false;
        }

        #endregion Public methods

        #region Public properties

        /// <summary>
        /// Gets a value indicating whether the ResetIdleTimer is in effect.
        /// </summary>
        public bool TimerEnabled
        {
            get{
                return this.m_Timer.Enabled;
            }
        }

        /// <summary>
        /// Gets or sets a value indicating how often (in miliseconds) the timer should be rese.t
        /// </summary>
        public int TimerInterval
        {
            get
            {
                return this.m_Timer.Interval;
            }
            set{
                this.m_Timer.Interval = value;
            }
        }

        #endregion Public properties

        private void Timer_Tick(object sender, EventArgs e)
        {
           ResetTimer();
        }

        
    }

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
G&M Dynamics, s.r.o.
Slovakia Slovakia
I work for Bee Mobile.

Web site: http://beemobile4.net
Facebook site: http://facebook.com/BeeMobile
YouTube Channel: http://youtube.com/beemobile4dotnet

Comments and Discussions