|
|
Please support our sponsor:
Timers & Timing in VB
I see lots of timing related questions on VB forums and so decided to create a seperate category for issues related to timing.
These may involve the Timer Control, API, and will probably contain some related issues like creating a pause. However, if you are looking
for code to format strings in 00:00 for example this is really more related to strings -and that's where you'll find it.
'----------------------------------
'Burt Abreu -One way to time a loop
'if you don't need millisecond resolution
'----------------------------------
Dim BeginTime As Date
Dim FinishTime As Date
Dim ElapsedTime As Long
'Get the beginning time
BeginTime = Now
Do
'Your loop code...
Loop
'Get the time after you exit the loop
FinishTime = Now
'Figure how many seconds between them
ElapsedTime = DateDiff("s", BeginTime, FinishTime)
'Display like this or with debug
lblStart.Caption = BeginTime
lblFinish.Caption = FinishTime
lblElapsed.Caption = "Elapsed time in seconds " & ElapsedTime
End Sub
'-----------------------------------
'Francis J. Loh Francis.Loh@unisys.com
'Instead of writing a Pause function with
'the timer, just use the API...
'-----------------------------------
'It should be clear that the Sleep API function
'freezes your app for the milliseconds specified
'completely. MS KB Article ID Q158175
'has more information about this.
'Basically, if you want to pause to allow your
'application to finish a process in it's thread
'use a timer loop with doevents. If you're trying
'to wait for an external process to end or just
'simply wait use Sleep.
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
And call it like so...
Sleep (3000) '// Will pause for 3 seconds
|