dcsimg
Results 1 to 5 of 5

Thread: close application when idle for a few minutes

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2016
    Posts
    60

    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?

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    40,345

    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)

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,599

    Re: close application when idle for a few minutes

    Quote Originally Posted by si_the_geek View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    40,345

    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.

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,599

    Re: close application when idle for a few minutes

    Quote Originally Posted by si_the_geek View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width