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