Solved

Exit idle application after 'n' minutes

Posted on 2004-03-27
10
438 Views
Last Modified: 2008-02-01
I want my vb application to exit if its left idle say for 5 minutes. ie either the user has moved to some other application like word etc or has simply left the machine idle. not using the timer control since I will have to use that in all the forms.
0
Comment
Question by:vijaygalani
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 5

Expert Comment

by:fadloun
ID: 10694235
place a timer control on an invisble form which will be loaded as long as your application is running ,,, and use a public variable to dermine the elpased time to close your application ,,,, if the user activate your application,keypress occure,or mouse click on any form reset the variable.

regards
0
 
LVL 1

Expert Comment

by:chandraboth
ID: 10694263
you can use API public const Sleep(?). just try that in module

-->Form:
private sub test()

call sleep= (second)

ohter statements

end sub
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 10694924
Add a Timer control to your form.  Then code below shows how to extend the time limit each time something occurs that you consider not "idle".  In the example below, any time the mouse is moved on the form or the current control changes, another 5 minutes is added to the timer.  

Any time you want to extend the timer another 5 minutes just make a call to the extendTime() sub.  Other examples of places you might want to call this from are TextBox keypresses, ListBox mousemoves, etc...

Regards,

Idle_Mind

Option Explicit

Private exitTime As Date
Private lastControl As Control

Private Sub Form_Load()
    extendTime
    Set lastControl = Me.ActiveControl
    Timer1.Interval = 100
End Sub

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    extendTime
End Sub

Public Sub extendTime()
    exitTime = DateAdd("n", 5, Now()) ' compute 5 minutes from now
End Sub

Private Sub Timer1_Timer()
    Dim curControl As Control
    Set curControl = Me.ActiveControl
    If Not (curControl Is lastControl) Then
        Set lastControl = curControl
        extendTime
    ElseIf Now() > exitTime Then
        Unload Me
    End If
End Sub
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 2

Expert Comment

by:jasonholland
ID: 10696442
Your initial request said "NO TIMER", this means you have to keep checking, or a loop.
You could also create a timer for the loop to pause each loop so you dont use too much CPU.

Set the TIMEOUT value, and your form will close.
You will need to reset the LAST_ACTIVE value on
whatever form events you want. (gotfocus, lostfocus, activate, etc...)
This will keep the form open.

Here is one way...

Create a module,
Cut and Paste:

Global LAST_ACTIVE As Long

Sub main()
    Form1.Show
End Sub




Create a Form
Cut and Paste:

Private Sub Form_Load()
Dim TERMINATE_APPLICATION As Boolean
Dim TIMEOUT As Long
'Dim LAST_ACTIVE As Long

'// timer is done in seconds
'// so 60 seconds = 1 minute, and 5 mintues = 60 * 5
'// do 5 minutes = 60 * 5 or 300 seconds
TIMEOUT = 10
LAST_ACTIVE = Timer
TERMINATE_APPLICATION = False

Form1.Show
Form1.Label1.Caption = Timer

Do While TERMINATE_APPLICATION <> True
    DoEvents
    If Timer > (LAST_ACTIVE + TIMEOUT) Then
        TERMINATE_APPLICATION = True
    End If
    DoEvents
Loop

    '// End the application
    End
End Sub



Hope this helps
Jason Holland
0
 
LVL 28

Accepted Solution

by:
Ark earned 125 total points
ID: 10699958
Hi
Take a look on my sample at http://www.freevbcode.com/ShowCode.Asp?ID=3297
>>Check if an Application is Idle for a Period of Time<<
PS. No timers :)
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 10701015
You could always use the SetTimer() and KillTimer() API's, which would have the callback in a global module.  You could also put the extendTime() sub in the global module.

Idle_Mind
0
 
LVL 28

Expert Comment

by:Ark
ID: 10701105
Actually, settimer/killtimer as well as VB timer control involved chrcking ALL controls for keyboard/mouse activity. It's OK if form has few controls, but when form has a lot of controls, you have to have all thouse textxxx_mousexxx events monitoring over your form.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question