|
-
May 19th, 2003, 04:20 PM
#1
Thread Starter
Junior Member
Timer Gurus - How to make a stable 30 second timer?
Here's my problem: I need to generate an event every 30 seconds, but I need the event to occur at the same time consistently (+- 1 second) - with respect to the system clock.
I've written a simple app that starts a 30 second timer at 15 seconds into a minute interval. Unfortunately, I have observed that it does not stay synchronized to this start time ... so how can I reliably generate an event every 15 seconds and 45 seconds into each minute ... for an entire day?
All suggestions are welcome!
Dave
Last edited by dwacker; May 19th, 2003 at 04:26 PM.
-
May 19th, 2003, 04:49 PM
#2
Fanatic Member
without getting too complicate, i would generate a regular timer event every second and i would only execute what i need to if the "seconds" part of the current system time is 15 or 45. you may still be missing 'heartbeats' (like, because of the drift of the timer control, it would miss a 'tick' on second 15 or 45). depending on how critical it is for your app, you may want to ignore this mishap, or you may want to provide the logic to make sure that the code i need gets executed at least once in the 15-44 interval, and at least once in the 45-59 and 0-14 interval.
if there is something that would prevent the timer to tick properely, it would be your application itself. in other words, a slow-executing sub or function in your own app may delay the timer event; same loop executing in another app will not affect your timer.
there are 2 reasons why i leave my work unfinished:
(1) i'm getting old.
-
May 19th, 2003, 05:07 PM
#3
Have a play with this:
VB Code:
Option Explicit
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Sub Form1_Load()
My_Timer
MsgBox "Times up"
End Sub
Private Sub My_Timer()
Dim tStart As Long
tStart = GetTickCount() \ 1000
Do
DoEvents
Loop Until GetTickCount() \ 1000 >= tStart + 5
End Sub
It'll display a MessageBox 5 seconds after Form1 load.
-
May 19th, 2003, 06:00 PM
#4
Lively Member
Here is some code for a timer control with its interval set to 1000.
VB Code:
Private Sub Timer1_Timer()
'The next second we gotta "tick" on
Static NextSecond As Long
'The second we're at now
Dim ThisSecond As Long
'This will get set to true on the 15th
'and 45th seconds
Dim IsTime As Boolean
'Get the current second
ThisSecond = Second(Now)
'Initialize NextSecond if it is zero
If NextSecond = 0 Then
If ThisSecond <= 15 Or ThisSecond > 45 Then
NextSecond = 15
Else
NextSecond = 45
End If
End If
'Determine whether it is time to "tick"
If NextSecond = 45 Then
If ThisSecond >= 45 Then
IsTime = True
NextSecond = 15
End If
Else 'NextSecond = 15
If ThisSecond < 45 And ThisSecond >= 15 Then
IsTime = True
NextSecond = 45
End If
End If
'If it's time, run your code
If IsTime Then
'put your code here that is to execute
'on the 15th and 45th seconds
End If
End Sub
-
May 19th, 2003, 06:59 PM
#5
Thread Starter
Junior Member
Thanks for the suggestions ... I'll play around with these ideas to see if I get satisfactory results.
Thanks,
Dave
-
May 19th, 2003, 08:06 PM
#6
Addicted Member
slap a timer on a form, with an interval low enough to make sure it fires at least once each second, and use code like this:
VB Code:
Private Sub Timer1_Timer()
Dim strSecond As String
Dim strLastTick As String
strSecond = Mid(Time$, 7, 2)
If strSecond = "15" Or strSecond = "45" And strLastTick <> strSecond Then
'your code here
strLastTick = strSecond
End If
End Sub
it's simple and it works 
hope it helps,
-
May 19th, 2003, 08:15 PM
#7
Fanatic Member
Originally posted by glyptar
slap a timer on a form, with an interval low enough to make sure it fires at least once each second, and use code like this:
[some code here]
it's simple and it works 
hope it helps,
... unless... if somewhere else in another part of the app i have a loop like this
VB Code:
For i = 0 to BiggestValueEver
Next i
and if the execution of this loop (and actually of all the call stack associated with it) takes more than 30 seconds, you will be definitely missing a beat. the code that tygur provided kinda makes sure that, even if the timer doesnt fire in the right second, at least it will pop up and execute sometime later, and will only execute once. supposedly, this is the desired effect
there are 2 reasons why i leave my work unfinished:
(1) i'm getting old.
-
May 19th, 2003, 08:23 PM
#8
Addicted Member
Your best bet is using the GETTICKCOUNT() api. It gets the number of miliseconds the system has been up. THis is preferable because the sytem timer has the highest priority in windows. so you're positive that it'll never be inaccurate. Well, don't kill the VB code either, kuz VB isn't lightening either!
-
May 19th, 2003, 09:16 PM
#9
Originally posted by INF3RN0666
Your best bet is using the GETTICKCOUNT() api.
Hmmmm, I thought that was what I posted earlier
-
May 19th, 2003, 10:09 PM
#10
I believe that you can prevent your own program from stopping your own timer by using the doevents command in loops. This stops stack build-up.
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
|