0

how can i catch when the form not used by the user i need to make form that will closed after 5 minutes if the user don't make any action on this form with C# Code Please help me

I make this code for closing system after 5 minutes

        Timer.Interval = (1000) * (300); 
        Timer.Enabled = true;
        Timer.Start();  

 private void Timer_Tick(object sender, EventArgs e)
        {
            Close(); 
        }

But i need to make this code active if the form not used .

leppie
  • 115,091
  • 17
  • 196
  • 297
ahmed mohamady
  • 293
  • 1
  • 7
  • 30
  • 1
    it might help you [this](http://stackoverflow.com/questions/5882658/how-to-detect-my-application-is-idle-in-c) – spajce Feb 12 '13 at 08:09
  • Add Timer.Start in your Forms LostFocus Event and in the Timer_Tick event calculate the time, if you find the time has exceeded 5 minutes close the form. – Searock Feb 12 '13 at 08:14

2 Answers2

0

write this.

    private void Form1_Activated(object sender, EventArgs e)
    {
        Timer.Enabled = false;
    }

    private void Form1_Deactivate(object sender, EventArgs e)
    {
        Timer.Enabled = true;
    }
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
0

Use the "Leave" event to manage your exit logic, like say trigger the timer.. The leave event gets fired on loosing focus.

r.sumesh
  • 313
  • 1
  • 3
  • 13
  • thank you for your answer, but I don't mean when the form loses focus and is no longer the active form. I mean if the form stile on focus but not used by the system user – ahmed mohamady Feb 12 '13 at 08:41
  • @ahmedmohamady then how do you mean "not used by user"? You say, the form is active and there is no activity on the system? like no keyboard/mouse activity? – r.sumesh Feb 12 '13 at 08:46
  • you know the screen server method? I need some thing like this but the event will be closing the form – ahmed mohamady Feb 12 '13 at 08:47
  • you may read thru [link](http://www.harding.edu/fmccown/screensaver/screensaver.html) or [link](http://www.codeproject.com/Articles/4809/How-to-develop-a-screen-saver-in-C) for ideas – r.sumesh Feb 12 '13 at 08:54