|
-
Jul 10th, 2007, 06:59 AM
#1
Thread Starter
New Member
Timer issue on VB6
I need to place some kind of timer on a form so that it closes if left open for 30 minutes. Ive checked the timer out and can see that this can only be configured for 65 seconds.
Can someone help with how i go about keeping a form open for up to 30 minutes after it Loads?
-
Jul 10th, 2007, 07:32 AM
#2
-
Jul 10th, 2007, 07:43 AM
#3
Re: Timer issue on VB6
...and after every elapsed minute you can add 1 to a variable that keeps track of the accumulated minutes and check if it is more than 30.
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
-
Jul 10th, 2007, 08:26 AM
#4
Re: Timer issue on VB6
Use the way opus described; it is better than counting.
-
Jul 10th, 2007, 09:08 AM
#5
Re: Timer issue on VB6
Then you need the Timer function (or GetTickCount) to check the elapsed time every minute or whatever the timer (control) interval has been set to.
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
-
Jul 10th, 2007, 10:16 AM
#6
Re: Timer issue on VB6
I was thinking of DateDiff, actually:
Code:
Option Explicit
Private mdtmLastActivity As Date
Private Sub Form_Load()
mdtmLastActivity = Now()
End Sub
Private Sub Timer1_Timer()
If DateDiff("n", mdtmLastActivity, Now()) >= 30 Then Unload Me
End Sub
Set the timer interval for anything between 10-60 seconds and you're good to go.
Last edited by Ellis Dee; Jul 10th, 2007 at 10:20 AM.
Reason: Changed variable name to LastActivity
-
Jul 10th, 2007, 10:19 AM
#7
Re: Timer issue on VB6
I notice the OP qualifies it with "if left open", meaning you don't want to close unless the user stopped responding. In that case, set the form's KeyPreview property to true, and add this:
Code:
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
mdtmLastActivity = Now()
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
mdtmLastActivity = Now()
End Sub
-
Jul 10th, 2007, 11:24 AM
#8
Thread Starter
New Member
Re: Timer issue on VB6
Thanks for all your help so far. I have tried the following...
 Originally Posted by Ellis Dee
I was thinking of DateDiff, actually:
Code:
Option Explicit
Private mdtmLastActivity As Date
Private Sub Form_Load()
mdtmLastActivity = Now()
End Sub
Private Sub Timer1_Timer()
If DateDiff("n", mdtmLastActivity, Now()) >= 30 Then Unload Me
End Sub
Set the timer interval for anything between 10-60 seconds and you're good to go.
...However the window closes after about 10 seconds.
Any Ideas?
-
Jul 10th, 2007, 12:04 PM
#9
Re: Timer issue on VB6
That's odd; the code should work. Try changing the timer code to this and see what the msgbox says:
Code:
Private Sub Timer1_Timer()
If DateDiff("n", mdtmLastActivity, Now()) >= 30 Then
MsgBox DateDiff("n", mdtmLastActivity, Now()) & " minutes elapsed"
Unload Me
End If
End Sub
-
Jul 10th, 2007, 01:13 PM
#10
Thread Starter
New Member
Re: Timer issue on VB6
Wierdly it says
56554272 minutes have elapsed
-
Jul 10th, 2007, 02:34 PM
#11
Re: Timer issue on VB6
Not weird just incorrect coding, you were actually checking for minutes with the "n" parameter. What you want is this:
Code:
Option Explicit
Private mdtmLastActivity As Date
Private Sub Form_Load()
mdtmLastActivity = Now()
Timer1.Interval = 1000
Timer1.Enabled = True
End Sub
Private Sub Timer1_Timer()
Dim Seconds As Long
Seconds = DateDiff("s", mdtmLastActivity, Now())
If Seconds >= 30 Then
Unload Me
End If
End Sub
or this
Code:
Option Explicit
Private mdtmLastActivity As Date
Private Sub Form_Load()
mdtmLastActivity = Now()
Timer1.Interval = 1000
Timer1.Enabled = True
End Sub
Private Sub Timer1_Timer()
Static Seconds As Long
Seconds = Seconds + 1
If Seconds >= 30 Then
Unload Me
End If
End Sub
-
Jul 10th, 2007, 04:10 PM
#12
Re: Timer issue on VB6
Ellis Dee's code is correct. Kusky is looking for an elapsed time of 30 minutes, not seconds.
 Originally Posted by kusky
Wierdly it says
56554272 minutes have elapsed
Apparently the mdtmLastActivity variable was either not initialized or not properly declared, as that number suggests that its value was zero (30 Dec 1899, 12:00:00 AM).
-
Jul 11th, 2007, 01:27 AM
#13
Re: Timer issue on VB6
Exactly why you should not use the date as a counter. If the date is reset the whole time changes. In the second way I posted a tick of the clock doesn't matter when the date is reset to anything.
-
Jul 11th, 2007, 02:59 AM
#14
Thread Starter
New Member
Re: Timer issue on VB6
I copied the code from elis again and this time it worked better(i must of declared the mdtmLastActivity incorectly), however i tested it for a minute and it closed after 34 seconds, any ideas why?
Code:
Option Explicit
Private mdtmLastActivity As Date
Private Sub Form_Load()
mdtmLastActivity = Now()
Timer1.Enabled = True
Timer1.Interval = 1000
End Sub
Private Sub Timer1_Timer()
Static Seconds As Long
Seconds = Seconds + 1
If DateDiff("n", mdtmLastActivity, Now()) >= 1 Then
Unload Me
MsgBox DateDiff("n", mdtmLastActivity, Now()) & " minutes elapsed"
Seconds = 0
End If
lblCount = Seconds
End Sub
-
Jul 11th, 2007, 03:02 AM
#15
Re: Timer issue on VB6
Possibly you should not use DateDiff in a timer...
-
Jul 11th, 2007, 03:18 AM
#16
Re: Timer issue on VB6
The code you (kusky) posted is a mixture of the code from EllisDee (datediff) and randem.
You create the Static Seconds and update it, but you don't use it for checking!
You use the DateDiff correctly, however you're telling the software to stop after one minute (which is something like 60 seconds AFAIK) and not 30 minutes. How did you stop the 34 seconds, exactly when the Form_lad was happening??
did you see the messagebox at all?, since this command is after Unload.Me I don't think so (it was the other way around in Ellis code!)
You're welcome to rate this post!
If your problem is solved, please use the Mark thread as resolved button
Wait, I'm too old to hurry!
-
Jul 11th, 2007, 04:28 AM
#17
Thread Starter
New Member
Re: Timer issue on VB6
 Originally Posted by opus
The code you (kusky) posted is a mixture of the code from EllisDee (datediff) and randem.
You create the Static Seconds and update it, but you don't use it for checking!
You use the DateDiff correctly, however you're telling the software to stop after one minute (which is something like 60 seconds AFAIK) and not 30 minutes. How did you stop the 34 seconds, exactly when the Form_lad was happening??
did you see the messagebox at all?, since this command is after Unload.Me I don't think so (it was the other way around in Ellis code!)
The reason my code has the seconds is simply for me to view something when im testing it.
I have found out that no matter when i open the form it will close when the system clock hits the first full minute. The Code does not wait for 1 minute after the form was opened.
IS the only way to make sure its full minutes to use what randem suggests and use seconds?
-
Jul 11th, 2007, 06:28 AM
#18
Re: Timer issue on VB6
Sorry I haven't read all the posts but in case it may help, just place a timer in your form with interval set to e.g. 1000 (1 second) and paste this code:
VB Code:
Option Explicit
Private Declare Function GetTickCount Lib "kernel32" () As Long
Dim StartTime As Long
Dim EndTime As Long
Private Sub Form_Load()
'Aplication must end after this
'time (in minutes) has elapsed
EndTime = 30
'Convert to milliseconds
EndTime = EndTime * 60 * 1000
'Elapsed milliseconds since windows was started
StartTime = GetTickCount
End Sub
Private Sub Timer1_Timer()
'Is the elapsed time since application started
'larger than the maximum allowed time?
If GetTickCount - StartTime >= EndTime Then
Unload Me
End
End If
End Sub
To avoid waiting, try to set EndTime to a small fraction of a minute to see how it works.
Last edited by krtxmrtz; Jul 12th, 2007 at 12:42 AM.
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
-
Jul 11th, 2007, 08:13 AM
#19
Re: Timer issue on VB6
 Originally Posted by kusky
I have found out that no matter when i open the form it will close when the system clock hits the first full minute. The Code does not wait for 1 minute after the form was opened.
IS the only way to make sure its full minutes to use what randem suggests and use seconds?
No, DateDiff will work fine, although if you think the user might change the system time then go with randem's counter technique.
DateDiff() returns a 1 the moment the unit of time has changed. If you ask DateDiff() the difference in years between 12/31 and 1/1, it'll tell you a year has passed when in reality only a day has.
What that means is that you want to use the next size down whenever the value you're looking for is less than 10. When looking for 30 minutes, checking the DateDiff() minutes differential is fine. When looking for only a single minute, however, you'll want to check for 60 seconds:
If DateDiff("s", mdtmLastActivity, Now()) >= 60 Then
I dislike using counters because timer controls are unreliable. If the system is busy, timer controls don't bother to fire an event. Even worse is that timers don't "save up" missed events; they just flat out skip them. If you use the counter method and set a timer's interval for 60,000 as a way of counting minutes, then you just have to hope that each minute when the timer is about to fire your program doesn't happen to be busy.
Of course, that's the whole point of this thread -- tracking user inactivity -- but this is why I just don't like relying on timers to do any kind of calculation in general. So instead of counting intervals, I always opt for comparing the current time with the starting time.
To illustrate timers skipping iterations, create a project, add a command button and a timer control, then paste this into the form's module:
Code:
Option Explicit
Private Sub Command1_Click()
MsgBox "Blocker - Timer1 is not firing"
End Sub
Private Sub Form_Load()
Timer1.Interval = 1000
Timer1.Enabled = True
End Sub
Private Sub Timer1_Timer()
Static lngCount As Long
lngCount = lngCount + 1
Print lngCount & " seconds elapsed"
End Sub
Run it, then after a few seconds have gone by click the command button. Wait a few more seconds and then click the OK button on the messagebox.
-
Jul 11th, 2007, 01:40 PM
#20
Addicted Member
Re: Timer issue on VB6
i use this code to keep track of time elapsed
vb Code:
Private Sub Timer1_Timer()
Dim secs As Long
Dim mins As Long
Dim hour As Long
secs = Val(Text1.Text + 1)
Text1.Text = secs
If Text1.Text = 60 Then
Text1.Text = 0
mins = Val(Text2.Text + 1)
Text2.Text = mins
End If
If Text2.Text = 60 Then
Text2.Text = 0
hour = Val(Text3.Text + 1)
Text3.Text = hour
End If
End Sub
you could do a check like this:
vb Code:
if text2.text = 30 then
unload me
end if
and put it on a timer
Last edited by masterkert3; Jul 11th, 2007 at 02:03 PM.
-
Jul 12th, 2007, 03:10 AM
#21
Thread Starter
New Member
Re: Timer issue on VB6
All works great now. Thanks everyone for your help. I went with Ellis suggestion and works like a charm - thanks again
-
Jul 16th, 2007, 05:34 AM
#22
Re: Timer issue on VB6
 Originally Posted by kusky
All works great now. Thanks everyone for your help. I went with Ellis suggestion and works like a charm - thanks again
Well, the code I posted in #18 is not having the problem that EllisDee refers to in #19 as it is based on the GetTickCount function. The timer may not fire but time is still being kept track of.
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
-
Jul 16th, 2007, 07:52 AM
#23
Re: Timer issue on VB6
 Originally Posted by krtxmrtz
Well, the code I posted in #18 is not having the problem that EllisDee refers to in #19 as it is based on the GetTickCount function. The timer may not fire but time is still being kept track of.
Same basic idea as what I suggested. Using DateDiff (or the Timer function, which has the same precision as GetTickCount) has the added bonus of being native VB, so no API declarations are required.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|