Windows form Inactivity

  • Thread starter Thread starter Bala Nagarajan
  • Start date Start date
B

Bala Nagarajan

Hi,
I want to implement someting like "Session Timeout" in my windows forms
application. How do i acheive this?
I want to be able to find out if the user has not interacted with my
application( Windows Forms) for a certain period of time and and inform the
user to relogin if the time expires. I looked at the Application.Idle event
but couldn't really figure how it could acheive what i wanted.


Thanks for the help.

Bala
 
Hi Bala,

Thanks for your post.

I think this is an interesting question. The implementation is based on
your program logic definition for "idle". For example, the "idle" maybe
mean a period of time without any mouse/keyboard input. However, I think it
is reasonable to ignore the WM_MOUSEMOVE messages, because even when your
form is in background, when the mouse cursor moves move your form window,
the WM_MOUSEMOVE message will be prompt to your application, this should
not be recognized as "active" state.

So, our design can use a timer component to calculate the idle time, and
any "active" mouse/keyboard messages can reset this timer to start again
from 0. Once the timer calculated without any active input for your setting
time(for example 20 minutes), we can ask the user for re-login.

In .Net winform, to get all the input messages, we can add a Message filter
on the main GUI thread(with IMessageFilter interface), then we can get
notification in IMessageFilter.PreFilterMessage method. In this method, we
can reset timer for any "active" input messages, such as WM_LBUTTONDOWN,
WM_RBUTTONDOWN, WM_KEYDOWN etc... Below is the sample code snippet:

static void Main()
{
Application.AddMessageFilter(new TestMessageFilter());
Application.Run(new Form1());
}

public class TestMessageFilter : IMessageFilter
{
private int WM_LBUTTONDOWN=0x0201;
private int WM_KEYDOWN=0x0100;
public bool PreFilterMessage(ref Message m)
{
// Blocks all the messages relating to the left mouse button.
if (m.Msg==WM_LBUTTONDOWN||m.Msg==WM_KEYDOWN||other active messages)
{
Console.WriteLine("Processing the messages : " + m.Msg);
//reset the timer.
}
return false;
}
}
Hope this helps

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi Jeffrey,
Thanks a lot for your elaborate and excellent answer. I think i will
try your idea and let you know if it resolves my issue.

Thanks for the support.

Bala
 
Ok, if there is still anything unclear, please feel free to tell me, thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Hi Bala,

Is your problem resolved? Is your still have any concern, please feel free
to tell me, thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top