Windows form Inactivity

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
 
J

Jeffrey Tan[MSFT]

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.
 
B

Bala Nagarajan

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
 
J

Jeffrey Tan[MSFT]

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.
 
J

Jeffrey Tan[MSFT]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top