Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Exit idle application after 'n' minutes

Posted on 2004-03-27
10
Medium Priority
?
447 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
7 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 500 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

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses
Course of the Month20 days, 20 hours left to enroll

810 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