4

Possible Duplicate:
Check if an application is idle for a time period and lock it

Suppose I have a Windows product developed with C#. Now a requirement comes that if that application running and idle and when user try to interact with that application again then a login screen come. How to detect that my applications is idle when it is running? Please guide me to complete the job.

Community
  • 1
  • 1
Mou
  • 15,673
  • 43
  • 156
  • 275
  • 3
    Dupicated http://stackoverflow.com/questions/1541981/check-if-an-application-is-idle-for-a-time-period-and-lock-it – Damian Leszczyński - Vash May 04 '11 at 11:37
  • I found few links, kindly Check them http://msdn.microsoft.com/en-us/library/system.windows.forms.application.idle.aspx http://blog.perfectapi.com/2008/05/detecting-idle-state-in-winforms-apps/ – SharpUrBrain May 04 '11 at 12:23
  • I think you may get the solution from below links [Application Idle state](http://www.codeproject.com/KB/cs/ApplicationIdle.aspx) [Windows forms application idle](http://msdn.microsoft.com/en-us/library/system.windows.forms.application.idle.aspx) [Detecting application idle state in windows forms](http://ellisweb.net/2008/02/detecting-application-idle-state-in-windows-forms/) – SharpUrBrain May 04 '11 at 12:34

4 Answers4

8
  1. Add a timer control to your application.
  2. Subscribe to mouseover and keydown events - when they fire, reset the timer.
  3. When the timer fires (ie mouse hasn't moved and key's haven't been pressed for x amount of time), lock the screen / prompt for login.
Nathan
  • 6,095
  • 2
  • 35
  • 61
  • 1
    Subscribing to the mouseover may fire the timer very frequently. Probably Keydown or mousedown events may be good to subscribe. – CharithJ May 04 '11 at 11:55
  • yes - you'll need to see what works best for your needs. (resetting the timer isn't expensive though, so don't worry about that) – Nathan May 04 '11 at 11:58
1

Here go my simple solution:

Point cursorPoint;
int minutesIdle=0;

private bool isIdle(int minutes)
{
    return minutesIdle >= minutes;
}

private void idleTimer_Tick(object sender, EventArgs e)
{
    if (Cursor.Position != cursorPoint)
    {
        // The mouse moved since last check
        minutesIdle = 0;
    }
    else
    {
        // Mouse still stoped
        minutesIdle++;
    }

    // Save current position
    cursorPoint = Cursor.Position;
}

You can setup a timer running on 60000 interval. By this way you will just know how many minutes the user don't move the mice. You can also call "isIdle" on the Tick event itself to check on each interval.

Tiago Gouvêa
  • 15,036
  • 4
  • 75
  • 81
  • 1
    This solution will not work to detect when a particular application has been idle. Because there is only one mouse cursor, your function will detect if the mouse has moved anywhere in the operating system. The OP wants to determine when a particular application has been idle--not when the OS has been idle. – Jazimov Jan 13 '20 at 22:41
1

You can use Control.LostFocus to record time when user navigated away then use Control.GotFocus to check how much time has passed to determine whether or not they need to log in.

TedTrippin
  • 3,525
  • 5
  • 28
  • 46
0

Capture the leave event of the Form class object to know it has lost focus.

FIre Panda
  • 6,537
  • 2
  • 25
  • 38