views:

40

answers:

3

Hi

Dows anyone know how I can build a timeout feature into a windows forms app.

The app is event driven, but I am thinking of somehow using a timer which counts down for say 10minutes, and one the timer ticks then we time out the user.

The problem I have is how can I reset the timer each time the mouse is moved or clicked.

Any help appreciated.

Cheers

A: 

In setting timer.Interval to 0 it does not work ?

Private Sub Form1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
    Me.Timer1.Stop()
    Me.Timer1.Start()
End Sub
Florian
HiAnd where would I start the timer again?
Richard
Also, how does this work where I have multiple modal forms?
Richard
I have edited the code
Florian
+1  A: 

you can use System.Windows.Forms.Timer.

you can drag it from your toolbox to the designer surface.

Use the properties window to set the Interval Property to the time span you want(miliseconds), the Enabled property should be set to false.

on the for load set the timer Enabled property to true.

(The event handler in the sample are written using c# - sorry about that)

private void Form1_Load(object sender, EventArgs e) { timer1.Enabled = true; }

Double click the timer tick event to register to the event, and close the form on the timer tick

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

Itai Zolberg
How does this work with modal forms?
Richard
if by 'modal' you mean `ShowDialog` and you are implementing this code in the main window (the one that is on the `Application.Run`) than the answer is - yes all modal windows will close too.
Itai Zolberg
Brilliant, thanks for your help
Richard
i'm happy i could help you
Itai Zolberg
A: 

As bad as it seems, I think the best way for that is to use a system.timer object with a set interval of a few milliseconds at most.

What I saw once is the use of a global variable that would get the time of the last action and that variable would be set to Now (using a global function for example) each time an action is performed. In your timer elapsed event, you check if now if bigger that the last action with your 10 minutes limit and act accordingly.

As for multi-form app, you could either use a different timer on each form , or only have the timer run on your main form.

Hope that helps.

David Brunelle