-
May 19th, 2012, 07:28 PM
#1
Most Accurate Delay Method
What is VB6's most accurate delay method? If I want to "delay" for 1573ms, what would I use to get as close as "1573ms" as possible?
Stopwatch, sleep, timer, etc?
-
May 20th, 2012, 01:30 AM
#2
Re: Most Accurate Delay Method
Not hard, as long as it is <20ms at a time.
Sleep(2000) will wait 2 seconds.
VB6 can't see values less than 20, but can do years in advance.
-
May 20th, 2012, 12:11 PM
#3
Re: Most Accurate Delay Method
Originally Posted by dglienna
Not hard, as long as it is <20ms at a time.
Sleep(2000) will wait 2 seconds.
VB6 can't see values less than 20, but can do years in advance.
Do you know what API sleep uses? Queryperformancecouter, gettickcount, etc?
Also, isn't it the same as Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) ?
-
May 21st, 2012, 01:16 AM
#4
Re: Most Accurate Delay Method
Almost all timings are linked to a hardware timer that is accurate to ~16ms. However this is still accurate enough for most applications, (unless you need 1ms accuracy)..
Now...
To your question..
Stopwatch : There is no such object by default in VB...
Timer : Accurate enough, however if the system is very busy, can be delayed by an unknown time. (although it is possible to calculate the delay after the fact.)
Sleep: will Pause the current application thread for the determined time, and is accurate to +- 16ms. Problem here is anything else running on this thread will not respond at all while waiting for the sleep timeout. (application will stop for that time)..
Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
WPF Articles : 3D Animation 1 , 2 , 3
Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.
-
May 21st, 2012, 07:25 AM
#5
Re: Most Accurate Delay Method
I'd like to bring up the SetTimer/KillTimer API here. Although I don't know about its accuracy (which seems to be pretty good, however), it does not have the flaws of the timer control and it does not hang the application. Good programming will use the time to at least perform some DoEvents while waiting for the timer elapse.
Put this code in a module to experiment with. The timer API will not work within a Form because of the CallBack routine. I have added a little sample to show how you use the timer API to replace the Sleep() API.
Code:
Option Explicit
Private Elapsed As Boolean
Public Declare Function SetTimer Lib "user32" ( _
ByVal hwnd As Long, ByVal nIDEvent As Long, _
ByVal uElapse As Long, ByVal lpTimerFunc As Long _
) As Long
Public Declare Function KillTimer Lib "user32" ( _
ByVal hwnd As Long, ByVal nIDEvent As Long _
) As Long
Private TimerID As Long
Private Sub StartTimer(ByVal IntervalTime as long)
TimerID = SetTimer(0, 0, IntervalTime, AddressOf TimerEvent)
End Sub
Public Sub TimerEvent(hwnd As Long, msg As Long, idTimer As Long, dwTime As Long)
'if it is a one shot then kill the timer here
KillTimer 0, TimerID
Elapsed = True 'signal that time is elapsed
'other wise the following is called in the interval you have specified
'timed code goes here
End Sub
'this is a sample 'sleep' replacement
Public Sub TSleep(SleepTime as Long)
StartTimer SleepTime
While Not Elapsed
DoEvents
Wend
End Sub
-
May 24th, 2012, 06:17 PM
#6
Re: Most Accurate Delay Method
yes, my brother wof example is really great .use setTimer library .
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|