0

I´ve created a WinForm application. Now I want to detect the moment when the user hasn´t made any interaction with that program (e.g. after 20 minutes). After this event I want to return to my login-form. How and especially where can I achieve this? I´ve got lots of different forms and controls and don´t want to modify too much.

Thanks. :)

Smurf
  • 83
  • 5
  • 2
    Have you done anything to implement it so far? – Ricky Jun 23 '14 at 11:40
  • First, I would like to know your application architecture. _I´ve got lots of different forms and controls_ doesn't give much idea about it. Second, if you have any idea in mind to achieve it then start implementing it and if you get stuck at some point then ask question by posting the code snippet. – Ricky Jun 23 '14 at 12:00
  • @GrantWinney Your first link might be of interest to me. Thanks. – Smurf Jun 23 '14 at 12:14
  • @Ricky If you like to know: I´ve got ten WinForms, each of them has a control. Each control has access to my TcpClient which is connected to a database. Sorry, but I didn´t know what kind of code snippet I could have given you for my question. – Smurf Jun 23 '14 at 12:17
  • Ok. I just wanted to know if you have any idea with which we can proceed. Anyways, I have answered your question. Cheers!! – Ricky Jun 23 '14 at 15:04

3 Answers3

0

Without seeing ANY code...

A quick and DIRTY way to go would be to implement a Timer that checks the mouse position every 20 minutes... It's unlikely that the user would have NOT moved the mouse if they are still being active.. (Unless you're controls can be completely controlled via the keyboard)

I believe you can check the mouse position via:

Cursor.Position.X and Cursor.Position.Y

poy
  • 10,063
  • 9
  • 49
  • 74
0

Without seeing ANY code...

  1. Create a class under your namespace with single public static DateTime property.
  2. Handle any mouse and keyboard event on each Form.
  3. In Event hadler update this property with current time.
  4. Add Timer, that will check this property each X time.

In addition: see how to hadle keybord events on form level and Keyboard Input in a Windows Forms Application

Lev Z
  • 742
  • 9
  • 17
0

On your main form add a timer

public static System.Timers.Timer g_timer = new System.Timers.Timer(20 * 1000 * 60);

Now add an event on form1 constructor as:

public Form1()
{
    InitializeComponent();
    g_timer.Elapsed += new System.Timers.ElapsedEventHandler(g_timer_Elapsed);
    g_timer.Start();
}

and the event handler as

private static void g_timer_Elapsed(object sender, EventArgs e)
{
    //Hide/ Close other forms and Show login form
}

Now write a function to reset the timer in main form

public static void resettimer()
{
    g_timer.Stop();
    g_timer.Start();
}

Now you can call form1.resettimer() function on each form's KeyPress event like this.

private void Form2_KeyPress(object sender, KeyPressEventArgs e) 
{
    form1.resettimer(); 
}
Ricky
  • 2,323
  • 6
  • 22
  • 22