-1

Possible Duplicate:
Detecting idle users in Winforms

I am working with a desktop application and i want to lock the application if the it is idle from a specific time, if there is any event fired then the expiry time should be reset. When the application locked then user have to again enter username and password if he want to login again in the application.

Right now I am working with timer control to handle this functionality but i don't know the right place to write the code for reset the expiry time.

Pleas tell me how i can do that job...

Community
  • 1
  • 1
Dev Kashyap
  • 425
  • 1
  • 5
  • 6
  • imho it's enough that windows have a screen saver and can lock the computer if necessary. Why do you want to add an extra layer that does the same thing? – jgauffin Sep 19 '11 at 12:17
  • 1
    @jgauffin: In the US, this requirement often exists for government regulatory compliance. – Gabe Sep 19 '11 at 12:30

2 Answers2

0

You could have a timer control that when fires hides all the forms, and produces a login box.

That should point you in the right direction - I won't just give you the code.

m.edmondson
  • 30,382
  • 27
  • 123
  • 206
  • How i will reset the expiry time.There is multiple controls at my forms, have i reset the expiry time at click event of every control. – Dev Kashyap Sep 19 '11 at 11:02
  • I've modified my answer as hiding the forms might make more sense, as soon as the correct password is entered you can show them again. – m.edmondson Sep 19 '11 at 11:09
0

Here is a very good example. Detecting Application Idleness

You could use Application.Idle Event also, but you will have to implement your own locking logic. In fact you can use that even for reset/start your ideal timer.

EDIT 1

Another good article and example.

EDIT 2

This function retrieves the time in seconds since last user input. However this is not specifically for your application but for windows. Look at here for more details. And here is a similar thread.

    static int GetLastInputTime()
    {
        int idleTime = 0;
        LASTINPUTINFO lastInputInfo = new LASTINPUTINFO();
        lastInputInfo.cbSize = Marshal.SizeOf( lastInputInfo );
        lastInputInfo.dwTime = 0;

        int envTicks = Environment.TickCount;

        if ( GetLastInputInfo( ref lastInputInfo ) )
        {
        int lastInputTick = lastInputInfo.dwTime;

        idleTime = envTicks - lastInputTick;
        }

        return (( idleTime > 0 ) ? ( idleTime / 1000 ) : 0);
    }
Community
  • 1
  • 1
CharithJ
  • 46,289
  • 20
  • 116
  • 131
  • The definition of "idle" in the `Application.Idle` event is "not busy processing", while the OP's definition of idle is "user is not interacting". – Gabe Sep 19 '11 at 12:48
  • If I read the docs correctly, this gives the last interaction for "the current desktop", not specifically "this one application". So if the user has this secure app started but works in some other app, this method will return "busy" instead of "idle". – Hans Kesting Sep 19 '11 at 13:11
  • @ Hans Kesting : Yes, your correct, LastInputInfo is for windows not for a particular application. – CharithJ Sep 19 '11 at 22:43