How can i implement a timer in a procedure in one of my modules so that i can impose a delay of 1 second?
Printable View
How can i implement a timer in a procedure in one of my modules so that i can impose a delay of 1 second?
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
Thanks.
Quote:
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
If you want a 1 second delay, this is the correct way of doing it:
It's much more accurate. I wonder if that other way is accurate at all..Code:Dim sngStartTime as Single
sngStartTime = Timer
Do Until Timer - sngStartTime > 1
DoEvents
Loop
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
No, Timer is counted in seconds.
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.
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.
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)
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).Quote:
at least I believe it is, anyway
Then, how do you use a Timer that uses little resources...? Karl Moore himself, uses Timers with DoEvents... I think :rolleyes:
Quote:
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).
Yes, Timer do use DoEvents to stop the system from freezing, but they don't use it for a method of timing.
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.
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