-
Feb 21st, 2018, 04:10 PM
#1
Thread Starter
Member
close application when idle for a few minutes
Hi guys juste need to know how to close my application after a few minutes of inactivity?
-
Feb 21st, 2018, 05:38 PM
#2
Re: close application when idle for a few minutes
Decide what action(s) are considered "activity", and keep track of when the last one happened. Use a timer to determine how long it has been since the last activity, and if it is too long then close the application.
In terms of keeping track of the last activity, use a DateTime variable declared in an appropriate place (for a single-form application, declare it at form-level) and in the events for the items you consider to be activity, assign DateTime.Now to the variable.
How you close the application depends on details of the application, but for a single-form application that can be as simple as closing the form (Me.Close)
-
Feb 21st, 2018, 11:48 PM
#3
Re: close application when idle for a few minutes
Originally Posted by si_the_geek
In terms of keeping track of the last activity, use a DateTime variable declared in an appropriate place (for a single-form application, declare it at form-level) and in the events for the items you consider to be activity, assign DateTime.Now to the variable.
I would do it differently to that. In that case, you'd have to use a Timer to test that variable anyway so I'd just use the Timer alone and do away with the variable. If you inactivity period was going to be 3 minutes, you would set the Interval of the Timer to (3 * 60 * 1000) and then you would reset the Timer each time an "activity" occurred, where resetting means calling Stop and then Start. When the Tick event is raised, it's time to exit.
-
Feb 22nd, 2018, 05:36 AM
#4
Re: close application when idle for a few minutes
That's a good way to do it, as it reduces clutter in terms of variables, makes the code in the timer simpler, and means you can get the expiry time to be accurate with minimal processing - but does add more lines of code for each activity (2 to reset the timer, instead of 1 to reset the variable).
The differences are fairly negligible, so it is basically a matter of personal choice, but the timer-only approach is probably better unless there are lots of activities.
-
Feb 22nd, 2018, 06:41 AM
#5
Re: close application when idle for a few minutes
Originally Posted by si_the_geek
2 to reset the timer, instead of 1 to reset the variable
I'd probably write a ResetTimer method, so one line to reset the Timer each, plus an extra four for the method itself.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|