|
-
Jul 11th, 2007, 04:28 AM
#1
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
#2
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
#3
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
#4
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
#5
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
#6
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
#7
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
|