Re: Application Idle

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Daniel Bass (DanielBass)
Date: 08/20/04


Date: Fri, 20 Aug 2004 13:36:33 +0100

Hey there,

I've done something similar for a user logging in and out of a device, below
is the code I used:

//------------------------------------------------------------------------------------------
[MESSAGEFILTER.CS]

using System;
using System.Windows.Forms;
using OpenNETCF.Windows.Forms;
using Microsoft.WindowsCE.Forms;

namespace MyApp
{
    /// <summary>
    /// Summary description for MessageFilter.
    /// </summary>
    public class MessageFilter : IMessageFilter
    {
        // resst the time out for user log-ins
        public bool PreFilterMessage(ref Message m)
        {
            if ( m.Msg != 275 ) // message of time out event
                AppData.m_timeOfLastAction = DateTime.Now;

            return false;
        }
    }
}

//------------------------------------------------------------------------------------------
[APPDATA.CS]

using System;

namespace MyApp
{
 /// <summary>
 /// Summary description for AppData.
 /// </summary>
    public class AppData
    {

        public static DateTime m_timeOfLastAction = DateTime.Now;

        //
        // other public global data and constants here

    }
}

//------------------------------------------------------------------------------------------
[FORM1.CS - snippet]
        static void Main()
        {
            try
            {

                MessageFilter msgFlter = new MessageFilter();
                ApplicationEx.AddMessageFilter ( msgFlter );
                ApplicationEx.Run(new FormMain());
            }
            catch ( Exception ex )
            {
                MessageBox.Show ( "Unhandled Exception occurred... " +
ex.Message, "Error" );
            }
        }

//
// other stuff
//

        private System.Windows.Forms.Timer timerSessionExpires;

        TimeSpan timeExpiry = TimeSpan.FromMinutes ( 30.0 );

        private void timerSessionExpires_Tick(object sender,
System.EventArgs e)
        {
            if ( !AppData.m_bLoggedIn ) // flag in AppData
            {
                AppData.m_timeOfLastAction = DateTime.Now;
                return;
            }

            if ( DateTime.Now.Subtract ( timeExpiry ) >
AppData.m_timeOfLastAction )
            {
                //
                // current session has expired, user needs to log in again

                AppData.LogOut(); // implemented in AppData

                AppData.m_timeOfLastAction = DateTime.Now;
            }

        }

//
// other stuff
//

//------------------------------------------------------------------------------------------

The first main difference in you version and mine is your PreFilterMessage
is inclusive, where as mine is exclusive, since I know the event message
number of the timer that's checking my last action, I exclude it.
The second is my object for message filtering is minimul in content and I
leave the working out time bits to the main form.

Hope that helps.

Daniel.

"MDB" <nospam> wrote in message
news:OigXuGfhEHA.3964@TK2MSFTNGP12.phx.gbl...
> Hello All, I am trying to create a timer using the OpenNetCf to find out
> when the application is on idle and have been having some problems. I
> have
> found some example code using VB
> (http://wiki.opennetcf.org/ow.asp?AppIdleTimer) however I am using C#.
> Here
> is what my code looks like. Does anyone see what I am doing wrong?
> using IMessageFilter = MyApp.AppIdleTimer;
>
> CLASS:
>
> public class AppIdleTimer
> {
>
> public delegate void TimeoutDelegate();
> public event TimeoutDelegate AppTimeOut;
> private System.Windows.Forms.Timer m_timer = new
> System.Windows.Forms.Timer();
>
> public void New(int TimeoutInterval)
> {
> m_timer.Tick +=new EventHandler(m_timer_Tick);
> m_timer.Interval = TimeoutInterval;
> m_timer.Enabled = true;
> }
>
> private void m_timer_Tick(object sender,EventArgs e)
> {
> AppTimeOut();
> Application.DoEvents();
> }
>
> private void Dispose()
> {
> m_timer.Enabled = false;
> }
>
> OpenNETCF.Windows.Forms.IMessageFilter.PreFilterMessage
> public void PreFilterMessage(ref Microsoft.WindowsCE.Forms.Message m)
> {
> switch(m.Msg.ToString())
> {
> case "&H200":
> case "&H201":
> case "&H202":
> case "&H203":
> case "&H204":
> case "&H205":
> case "&H206":
> case "&H207":
> case "&H208":
> case "&H209":
> case "&H100":
> case "&H101":
> case "&H102":
> case "&H103":
> case "&H104":
> case "&H105":
> case "&H106":
> case "&H107":
> case "&H108":
> {
> m_timer.Enabled = false;
> m_timer.Enabled = true;
> break;
> }
> }
> }
> }
>
>
> INSIDE THE MAIN
>
> AppIdleTimer appIdle = new AppIdleTimer();
> appIdle.New(10000);
> appIdle.AppTimeOut += new
> MyApp.AppIdleTimer.TimeoutDelegate(appIdle_AppTimeOut);
>
> *** Getting This Error
> ***The best overloaded method match for
> 'OpenNETCF.Windows.Forms.ApplicationEx.AddMessageFilter(OpenNETCF.Windows.Fo
> rms.IMessageFilter)' has some invalid arguments
> ***Here
>
> ApplicationEx.AddMessageFilter(appIdle);
> ApplicationEx.Run();
>
>
>
>
> private void appIdle_AppTimeOut()
> {
> MessageBox.Show("TimeOut");
> }
>
>



Relevant Pages