Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Timer? How to set a program to end due to inactivity 3

Status
Not open for further replies.

terry22

Technical User
May 24, 2004
30
US
Hi,

How do I use/set a timer control to automatically end the program after 10 minutes of inactivity?

Thanks!
 
Terry,

How would you define inactivity?
For instance, no buttons on the form being clicked, no mouse movement on the form, no mouse movement period or no keyboard activity??



jgjge3.gif
[tt]"Very funny, Scotty... Now Beam down my clothes."[/tt]
 
Ahh, :eek:)

That's a very good question. Since the project is mostly a button click event based project, I suppose on click.

I've already got some code, that seems to be working. Is there some form based event that fires every time the form or, some object on the form is clicked/touched? For instance, I tried the form_activate, but that fires only the first time the form is shown. Or, will I need to place the timer reset code into EVERY click event in my code? ( After playing around with this code, I guessing it's the second! )

Or, perhaps, a mouse over would do the trick? I'm not really sure where the best place for the reset code is? What do you recommend?

Also, the milli-second thing is somewhat beyond my pathetic understanding of the timer minutes ... 60000 is 10 minutes, isn't it? And, 6000 is 1 minute?

Thank you for your help ... and also pointing out the many ways a person can look at this, as I was only looking at "click" as a possibility.

And, one last thought ... is there some way when the program does time out to close/unload everything except for that last message telling the person that the program has timed out?

Here is what I currently have:
Code:
'In the module:
'''''''''''''''''''''''''''''''''''''''''''''''''''''''
'for the timeout function
Public g_intMinute As Integer

Public Function SetProgramEndTimer()
    'reset/set/initialize program timer for time-out function due to inactivity

    g_intMinute = 0
    
    'set the timer for 10 minutes
    frmMenuMain.Timer1.Interval = 60000

    'enable the timer to keep track
    frmMenuMain.Timer1.Enabled = True

End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''

'In first menu - Main the timer code:
'''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Sub Timer1_Timer()

    g_intMinute = g_intMinute + 1
    
    If g_intMinute = 10 Then

        ' turn off the timer
        Timer1.Enabled = False
               
        MsgBox "The program has ended due to inactivity." & _
                Chr(13) & Chr(13) & "Please login again.", vbInformation

        'end program
        End

    End If
End Sub
 
first of all to convert milli-seconds to seconds divide by 1000. Then if u want to convert to minutes divide by 60.

Example: interval=60000 so u would do 60000/1000 = 60 seconds, 60/60 = 1 minute.


as for the other questions i dont think i can help as much.
To close everything, however, while still showing the msgbox you could use unload form1 (do it with all forms u have open).
after the unload statements put your msgbox.

 
So, if I understand you correctly...

this line:

'set the timer for 10 minutes
frmMenuMain.Timer1.Interval = 60000

is not 10 minutes but, 1 minute?

Therefore, 600000 would be the 10 minutes I seek ... is that right?

Also, since I have no mechanism for checking whether or not a form has been loaded and is now hidden, is there some way to check the state of the form to determine IF it is load and if so, then unload?
 
Terry,

It probably would be better if you used your g_intMinute variable as a flag to stop the first run of the timer and then let the timer run it's course..

Code:
Public Sub Timer1_Timer()

    If g_intminute > 0 then
        ' turn off the timer
        Timer1.Enabled = False
    
        'Hide all visible forms
        For Each Form in [i]ProjectName[/i]
            Form.Hide
        Next

        'Display MsgBox 
        '(vbCrLf is a Carriage Return Line Feed Constant
        MsgBox "The program has ended due to inactivity." & _
        vbCrLf & "Please login again.", _
        vbInformation + vbOKOnly, _
        "Inactivity Termination"

        'end program
        End 
    End If
    
    'Increment g_intMinute
    g_intMinute = g_intMinute + 1

End Sub

Now, it does looks like you will have to place the timer reset code into every click event on the buttons, however, if you simply write a sub-routine or function to do it for you (something simple) and then call it under each click event.

Code:
Public Sub TRS()
    'Reset Flag variable
    g_intMinute = 0
   
    'De- and Re-initialise the Timer
    [i]Timer1[/i].Enabled = False
    [i]Timer1[/i].Enabled = True

End Sub

and then on the click events...

Code:
Private Sub cmdSueMe_Click()
   Call TRS

   ....Rest of Click Event....

End Sub

Hope this helps.


jgjge3.gif
[tt]"Very funny, Scotty... Now Beam down my clothes."[/tt]
 
And to Clear up the Timer issue...

Since 1000 Milliseconds make a Second
There are 60 Seconds in a minute, Therefore
60 Seconds or 1 minute Expressed in Milliseconds would be
60 x 1000 = 60000
and being 60000 milliseconds in a minute, 10 minutes would be
60000 x 10 = 600000.

Hope this helps too.

jgjge3.gif
[tt]"Very funny, Scotty... Now Beam down my clothes."[/tt]
 
Wow, okay

... I see what you're doing. Keeping minutes = 0 instead of counting up to 10. Then if/when it gets to the amount of time the timer has been set to, since minutes is > 0 the End part of the program will fire.

That's Great!

What an elegant solution! Thank you very much, JAG! I wish I could give more stars.

I appreciate your help and thought.



 
Okay,

thanks again, the milli second thing is always (blush) a bit confusing for me!

So, 600000 is the 10 minute mark.

Can the timer handle that large a number w/o problems?
 
from the vb help files:

The interval can be between 0 and 64,767, inclusive, which means that even the longest interval can't be much longer than one minute (about 64.8 seconds).

So, I suppose I need to create a variable to look for the 600000 milli second mark.

Terry

 
The easy way of using a Timer control for more than one minute is to use a Static variable in the Timer event. This example is set for 10 minutes if the Timer1.Interval is set to 60000:
[tt]
Private Sub Timer1_Timer()
Static temp
If temp = 10 Then
MsgBox "It's time"
temp = 0
Else
temp = temp + 1
End If
End Sub
[/tt]

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
Thanks, John!!

I messed around with it until I figured out where to set the interval.

Actually, used code from one of your earlier posts to get me started - so, thanks for being so prolific and helpful.

I ended up changing:

If g_intminute > 0 then

to:
If g_intminute >= 10 then

this set the intervals.

It's working like a charm now.
 
Can anyone please guide me; I am a novice when it comes to VBA, however, I have been able to use it to my satisfaction in some Excel 2003 spreadsheets.

What I am trying to accomplish is to automatically shut down a spreadsheet after a period of inactivity.

Can someone please guide me.

Thank you!!
 
Hi and welcome to Tek-Tips. To get the best from these forums, please read faq222-2244.

You will find that it's best to ask a new question in a new thread of your own, rather than tag onto a one year old post, and your VBA questions will probably be more accurately answered in the VBA forum (forum707). This forum deals with VB5/6 and there are several important differences that will catch you out.

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Essex Steam UK for steam enthusiasts
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top