Results 1 to 14 of 14

Thread: Timer in Module

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2000
    Posts
    71

    Question Timer in Module

    How can i implement a timer in a procedure in one of my modules so that i can impose a delay of 1 second?

  2. #2
    Member
    Join Date
    Mar 2000
    Location
    Galt, Ca
    Posts
    47

    Arrow use a for next statement

    paste this into your module

    For a = 0 To 1000
    b = a / 16.66667
    Me.Caption = b
    DoEvents
    Next

    when b = 60 an interval pretty close to 1 second has passed.
    i don't know how to get exactly one second, but this is close.

    if you want it to repeat then put a do infront of it and a loop at the end

    the speed will vary based on your platform, that is the downside to not having a timer

    -emptywords

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Dec 2000
    Posts
    71

    Re: use a for next statement

    Thanks.
    Originally posted by emptywords
    paste this into your module

    For a = 0 To 1000
    b = a / 16.66667
    Me.Caption = b
    DoEvents
    Next

    when b = 60 an interval pretty close to 1 second has passed.
    i don't know how to get exactly one second, but this is close.

    if you want it to repeat then put a do infront of it and a loop at the end

    the speed will vary based on your platform, that is the downside to not having a timer

    -emptywords

  4. #4
    Tygur
    Guest
    If you want a 1 second delay, this is the correct way of doing it:
    Code:
    Dim sngStartTime as Single
    sngStartTime = Timer
    Do Until Timer - sngStartTime > 1
    DoEvents
    Loop
    It's much more accurate. I wonder if that other way is accurate at all..

  5. #5
    Addicted Member
    Join Date
    Apr 2000
    Location
    England
    Posts
    246
    Timer is based on milliseconds

    so that
    Code:
    Dim StopModTimer as Boolean 'Global variable
    
    Sub ModTimer(interval as long)
    
    dim Temp as long
    
    temp = timer + interval
    StopModTimer = false
    
    do Until stopModtimer = true
         if temp > timer then
              'Do Code
              temp = timer + interval
              DoEvents
         end if
    loop
    
    Exit Sub 
    
    Sub  modStopTimer()
         stopModTimer = true 
    Exit sub

  6. #6
    Megatron
    Guest
    No, Timer is counted in seconds.

  7. #7
    Megatron
    Guest
    And for the record, Tygur, there is no "correct way of doing it." There are lots of other methods that are also correct, e.g: GetTickCount, QueryPErformanceCounter, Sleep, and SetTimer.

  8. #8
    jim mcnamara
    Guest
    Actually, the above comment by Megatron should be expanded to most kinds of reasonable algorithms/methods.

    Some are short & easy to read, others more efficient, etc. They all have tradeoffs. Pick one you can live with and keep using it.

  9. #9
    Tygur
    Guest
    yeah i understand that there are many ways of doing many things. All of those ways you mentioned would've been "correct". Maybe I should've said "a more correct way" or just "a correct way"? I didn't think my wording mattered.

    I was just saying that assuming that DoEvents is going to take a specific ammount of time is incorrect. (at least I believe it is, anyway)

  10. #10
    Megatron
    Guest
    at least I believe it is, anyway
    Yes I'll agree with you on this one; DoEvents shouldn't be used for a Timer, because all it does is do other tasks (thus the time varies).

  11. #11
    Addicted Member cyberwarpy's Avatar
    Join Date
    Jan 2001
    Location
    Melbourne, Australia
    Posts
    200

    Question

    Then, how do you use a Timer that uses little resources...? Karl Moore himself, uses Timers with DoEvents... I think


    Originally posted by Megatron


    Yes I'll agree with you on this one; DoEvents shouldn't be used for a Timer, because all it does is do other tasks (thus the time varies).

  12. #12
    Megatron
    Guest
    Yes, Timer do use DoEvents to stop the system from freezing, but they don't use it for a method of timing.

  13. #13
    Addicted Member
    Join Date
    Apr 2001
    Posts
    250
    Try using the sleep API. I find it the most useful.

    Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

    Public Sub Sleepy(SnoozeTime as long)
    Sleep(SnoozeTime)
    End sub


    SnoozeTime is the amount of time the WHOLE pogram will stop for in milliseconds. It's not good if you still want a section to run during the sleep.
    Why does everyone think I may be dangerous? I'm just good at computers.

  14. #14
    Frenzied Member Microbasic's Avatar
    Join Date
    Mar 2001
    Posts
    1,402
    I find SetTimer most useful (although I don't use 30 timers anymore)

    Code:
    Public Declare Function SetTimer Lib "user32" Alias "SetTimer" (ByVal hWnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
    
    Public Declare Function KillTimer Lib "user32" Alias "KillTimer" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
    Sub TimerStart()
      SetTimer 0, 0, 1, AddressOf TimerProc
    End Sub
    Sub TimerStop()
      KillTimer 0, 0
    End Sub
    Sub TimerProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal id As Long, ByVal SysTime As Long)
      'Do Something
    End Sub


    MicroBasic
    Dragon Shadow Trainer

    There is no good or evil in the world...only programmers and fools .

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