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.

3

4 Answers 4

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.
2
  • 1
    Subscribing to the mouseover may fire the timer very frequently. Probably Keydown or mousedown events may be good to subscribe.
    – CharithJ
    May 4, 2011 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 4, 2011 at 11:58
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.

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.

1
  • 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, 2020 at 22:41
0

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.