The batch code below is presented as a curiosity. It shows that computers
are not perfect for delays, but just good enough if you don't have to
worry about gaining a few extra centiseconds in the delay.
The code writes a VBScript file and calls it to produce the delay.
If you enter 500 500 500 500 at the prompt, you should see differences in
the centiseconds.
I've gone through this whole post and I am assuming that no one found
a method to get consistent and exact delays...or did I miss it?
Thanks to Aacini for posting code on SO, which I used for converting the time to milliseconds.
Code: Select all
@Echo Off
Setlocal EnableDelayedExpansion
Echo Option Explicit>timedelay.vbs
Echo Dim WshArgs>>timedelay.vbs
Echo Set WshArgs = WScript.Arguments>>timedelay.vbs
Echo WSCRIPT.SLEEP WshArgs.Item(0)>>timedelay.vbs
Echo Display starting time, ending time and delay time after entering milliseconds
Echo to send as arguments to VBScript that will use the WSCRIPT.SLEEP
Echo command to produce a delay. You should see that centiseconds can vary for
Echo the same number of milliseconds entered multiple times.&Echo(
Echo Your entry may be more than one numeric value separated by a space or comma.&Echo(
Echo Just press Enter to quit.&Echo(
:top
Set "rsp="
Set /p rsp=^>^>^>
If "%rsp%"=="" Exit /b
Set "rsp=%rsp: =,%"
Set "rsp=%rsp:,,=,%"
For %%n In (%rsp%) Do (
Call:parseNumeric "%%n" isValid
If "!isValid!"=="TRUE" (
Set "startTime=!time!"
start /wait wscript.exe //b timedelay.vbs %%n
Set "endTime=!time!"
rem Get elapsed time:
Call:convertTime "!startTime!" msStart
Call:convertTime "!endTime!" msEnd
Set /A elapsed=msEnd-msStart
Echo !startTime! - !endTime! - !elapsed!
)
)
GoTo:top
::_______________________________________functions_____________________________________
:parseNumeric
rem return TRUE if %1 contains only numeric characters, otherwise return FALSE
setlocal EnableDelayedExpansion
Set "temp=%~1"
:pnLoop
Set "char=%temp:~0,1%"
Set "isValid=FALSE"
For %%c In (0 1 2 3 4 5 6 7 8 9) Do If %%c==!char! (Set "isValid=TRUE"&GoTo breakPN)
:breakPN
Set "temp=%temp:~1%
If "%temp%"=="" GoTo:endPNLoop
GoTo:pnLoop
:endPNLoop
endlocal&set "%~2=%isValid%"&exit /b
:: * * * * * * * * * * * * * * * * * * * * * * * * * *
:convertTime
rem return the time in milliseconds
Setlocal EnableDelayedExpansion
Set "_time=%~1"
For /F "tokens=1-4 delims=:.," %%a In ("%_time%") Do (
Set /A "currTime=(((%%a*60)+1%%b %% 100)*60+1%%c %% 100)*100+1%%d %% 100"
)
endlocal&set "%2=%currTime%"&exit /b