Click to See Complete Forum and Search --> : Timer in Module
eng70640
May 4th, 2001, 01:29 AM
How can i implement a timer in a procedure in one of my modules so that i can impose a delay of 1 second?
emptywords
May 4th, 2001, 01:59 AM
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
eng70640
May 4th, 2001, 02:11 AM
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
Tygur
May 4th, 2001, 02:35 AM
If you want a 1 second delay, this is the correct way of doing it:
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..
Nirces
May 4th, 2001, 05:50 AM
Timer is based on milliseconds
so that
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
Megatron
May 4th, 2001, 02:42 PM
No, Timer is counted in seconds.
Megatron
May 4th, 2001, 02:47 PM
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.
jim mcnamara
May 4th, 2001, 03:06 PM
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.
Tygur
May 4th, 2001, 03:13 PM
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)
Megatron
May 4th, 2001, 05:01 PM
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).
cyberwarpy
May 5th, 2001, 12:56 AM
Then, how do you use a Timer that uses little resources...? Karl Moore himself, uses Timers with DoEvents... I think :rolleyes:
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).
Megatron
May 5th, 2001, 11:44 AM
Yes, Timer do use DoEvents to stop the system from freezing, but they don't use it for a method of timing.
KamiKazeKiwi3
May 15th, 2001, 08:48 PM
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.
Microbasic
May 15th, 2001, 10:03 PM
I find SetTimer most useful (although I don't use 30 timers anymore)
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
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.