Solved

Exit idle application after 'n' minutes

Posted on 2004-03-27
10
433 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
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 85

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 27

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 85

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 27

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

947 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now