Tue 27 Jan 2009

Today I was required to implement detecting the idle time or user inactivity in windows form. Here it is how it works
The theme behind is the usage of MessageFiler that will be intercepting calls (Keyboard / Mouse activities) and usage of System.Windows.Forms.Timer

Step 1.

Create MessageFilter class as

using System;
using System.Windows.Forms;

 public class MessageFilter : IMessageFilter
    {
       //Following are the Windows API hex values. You can find more at http://faisal.azmza.com/post/2009/01/Values-of-Windows-API.aspx
       // Here we are only interested in only Keyboard and Mouse activities
        private int WM_LBUTTONDOWN = 0x0201;
        private int WM_KEYDOWN = 0x0100;
        private int WM_RBUTTONDOWN = 0x0204;
        private int WM_MBUTTONDOWN = 0x0207;
        private int WM_MOUSEWHEEL = 0x020A;
        private int WM_MOUSEMOVE = 0x0200;

        public bool PreFilterMessage(ref Message m)
        {
            if (m.Msg == WM_MOUSEMOVE || m.Msg == WM_KEYDOWN || m.Msg == WM_LBUTTONDOWN || m.Msg == WM_MOUSEWHEEL || m.Msg == WM_RBUTTONDOWN || m.Msg == WM_MBUTTONDOWN)
            {
                //Reset the timer of form1
                Form1.timerIdle.Stop();
                Form1.timerIdle.Start();
            }
            return false;
        }
    }

Step 2:

In the form where you actually want to implement what to do if user is inactive for particular period, like redirecting to Login page, performing Session / objects cleanup, hiding or locking touchy form etc etc, put following peace of code,

a) Declare static timer as

//Reason for taking this timer as static becasue we need to reset timer in MessageFilter class.
//If don't want to use static then you can implement your own logic. The theme is to reset the timer in MessageFilter on any Keyboard or Mouse activity

internal static Timer timerIdle;

b) Put following initializing code after InitializeComponent or on Form_Load. Note to set Interval of timer to your requirement. Here it is set for 5 minutes.

            timerIdle = new System.Windows.Forms.Timer();
            timerIdle.Enabled = true;
            timerIdle.Interval = 50000; // Idle time period. Here after 5 minutes perform task in  timerIdle_Tick
            timerIdle.Tick += new EventHandler(timerIdle_Tick);

 c) Put following inside your form

        private void timerIdle_Tick(object sender, EventArgs e)
        {
            //Here perform your action by first validating that idle task is not already running.
           // If you want to redirect user to login page, then first check weather login page is already displayed or not
           // if not then show loign page. Same logic for other task or Implement your own.
           // Remember after every five minutes or period you defined above this timerIdle_Tick will be called
           //so first check weather idle task is already running or not. If not then perform
            //if (Login.Visible == false)
            //{
                   //PerformNecessoryActions();
                  //ShowLoginForm();
            //}
        }

 

Step 3.

So far we have created MessageFilter class (On keyboard or mouse activity it will reset the timer of form). In form we have declared and initialize timer. On timer_Tick event we have implement the task which we need to do when user become inactive for particular period of time. Now in last step, Add our MessageFilter class in Application to intercept calls as

static void Main(string[] args)
{
            Application.AddMessageFilter(new MessageFilter());
            Application.Run(new Form1());
}


Hope it helps.

 

Comments (1)

Comments

5/12/2009 | blabla  
50000 milliseconds != 5 Minutes

In fact, that's ~ 1 hour and 22 minutes and 48 seconds


Add Comment Post comment

 
 
 
   Country flag

Loading

Captcha



Next Post  Previous Post : Clock ticking on worm attack code
Previous Post  Next Post : Values of Windows API


Ads