Results 1 to 10 of 10

Thread: Timer Gurus - How to make a stable 30 second timer?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2000
    Posts
    20

    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.

  2. #2
    Fanatic Member
    Join Date
    Feb 2003
    Location
    Los Angeles, CA
    Posts
    681
    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.

  3. #3
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    Have a play with this:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function GetTickCount Lib "kernel32" () As Long
    4.  
    5. Private Sub Form1_Load()
    6.     My_Timer
    7.     MsgBox "Times up"
    8. End Sub
    9.  
    10. Private Sub My_Timer()
    11.     Dim tStart As Long
    12.  
    13.     tStart = GetTickCount() \ 1000
    14.  
    15.     Do
    16.         DoEvents
    17.     Loop Until GetTickCount() \ 1000 >= tStart + 5
    18.  
    19. End Sub

    It'll display a MessageBox 5 seconds after Form1 load.

  4. #4
    Lively Member Tygur's Avatar
    Join Date
    Jul 2002
    Posts
    108
    Here is some code for a timer control with its interval set to 1000.
    VB Code:
    1. Private Sub Timer1_Timer()
    2. 'The next second we gotta "tick" on
    3. Static NextSecond As Long
    4. 'The second we're at now
    5. Dim ThisSecond As Long
    6. 'This will get set to true on the 15th
    7. 'and 45th seconds
    8. Dim IsTime As Boolean
    9.  
    10. 'Get the current second
    11. ThisSecond = Second(Now)
    12.  
    13. 'Initialize NextSecond if it is zero
    14. If NextSecond = 0 Then
    15.     If ThisSecond <= 15 Or ThisSecond > 45 Then
    16.         NextSecond = 15
    17.     Else
    18.         NextSecond = 45
    19.     End If
    20. End If
    21.  
    22. 'Determine whether it is time to "tick"
    23. If NextSecond = 45 Then
    24.     If ThisSecond >= 45 Then
    25.         IsTime = True
    26.         NextSecond = 15
    27.     End If
    28. Else 'NextSecond = 15
    29.     If ThisSecond < 45 And ThisSecond >= 15 Then
    30.         IsTime = True
    31.         NextSecond = 45
    32.     End If
    33. End If
    34.  
    35. 'If it's time, run your code
    36. If IsTime Then
    37.     'put your code here that is to execute
    38.     'on the 15th and 45th seconds
    39. End If
    40. End Sub

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Mar 2000
    Posts
    20
    Thanks for the suggestions ... I'll play around with these ideas to see if I get satisfactory results.

    Thanks,

    Dave

  6. #6
    Addicted Member glyptar's Avatar
    Join Date
    Sep 2002
    Location
    The Netherlands
    Posts
    138
    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:
    1. Private Sub Timer1_Timer()
    2.     Dim strSecond As String
    3.     Dim strLastTick As String
    4.  
    5.     strSecond = Mid(Time$, 7, 2)
    6.     If strSecond = "15" Or strSecond = "45" And strLastTick <> strSecond Then
    7.         'your code here
    8.        
    9.         strLastTick = strSecond
    10.     End If
    11. End Sub

    it's simple and it works

    hope it helps,
    Glyptar

  7. #7
    Fanatic Member
    Join Date
    Feb 2003
    Location
    Los Angeles, CA
    Posts
    681
    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:
    1. For i = 0 to BiggestValueEver
    2. 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.

  8. #8
    Addicted Member
    Join Date
    Mar 2002
    Posts
    229
    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!

  9. #9
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    Originally posted by INF3RN0666
    Your best bet is using the GETTICKCOUNT() api.

    Hmmmm, I thought that was what I posted earlier

  10. #10
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628
    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.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width