Solved

Exit idle application after 'n' minutes

Posted on 2004-03-27
10
432 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
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…

757 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

19 Experts available now in Live!

Get 1:1 Help Now