Solved

Exit idle application after 'n' minutes

Posted on 2004-03-27
10
435 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

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

830 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