How to "Wait" in Millisecond intervals

Example scripts and tips (replaces Old Scripts & Tips archive)

Moderators: Dorian (MJT support), JRL, Phil Pendlebury

Post Reply
User avatar
JRL
Automation Wizard
Posts: 3332
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

How to "Wait" in Millisecond intervals

Post by JRL » Wed Jun 17, 2020 7:33 pm

The Macro Scheduler Wait> function can be told to pause for a time interval as small as 0.001 seconds (one millisecond) However, the Wait> function can't actually pause for a time interval smaller than about 16 milliseconds. In other words you can type into your script "Wait>0.001", but the time the script pauses will not be 1 millisecond it will actually be 16 milliseconds, or more accurately 1/64th of a second which is 15.625 milliseconds.

If you truely need to wait for a time interval in milliseconds, you can instead use the Windows API "Sleep" function. "LibFunc>Kernel32,Sleep,Sres,1" will cause a Macro Scheduler script to pause for 1 millisecond.

The following script is a proof of this statement. The script will initially run 100 cycles of a Wait> function that starts at 1 millisecond then will increment upward at 1 millisecond intervals. The timed results of each 100 cycle loop will display in a message box. You will see that every 1 millisecond increment will run 100 Wait> cycles in about 1600 milliseconds until you reach a 16 millisecond pause at which point each loop of 100 Wait> cycles will take about 3200 milliseconds. When you reach 32 milliseconds per pause the 100 cycles loop time will take about 4500 milliseconds. Etc.

When you close the message box the script will move on to perform the same process but will use Windows API Sleep rather than Wait. As you watch the Sleep function pause time increment you will also see that the 100 cycle loop time increments as well. This indicates that the Windows API Sleep function actually pauses the script in millisecond increments.

Code: Select all

Message>Waiting...
Let>vWTime=0.000
Let>vContinue=1

While>vContinue=1
  Let>vWTime=%vWTime%+0.001
  GoSub>TimeOut
  IfWindowOpen>Macro Scheduler Message
  Else
    Let>vContinue=0
  EndIf
EndWhile

SRT>TimeOut
  Timer>vStartTime
  Let>kk=0
  Repeat>kk
    Add>kk,1
    Wait>vWTime
  Until>kk=100
  Timer>vEndTime
  Let>vTime=%vEndTime%-%vStartTime%
  SetControlText>Macro Scheduler Message,TMemo,1,Wait Time = %vWTime% seconds%crlf%100 cycles took %vTime% miliseconds
END>TimeOut


Message>Waiting...
Let>vWTime=0
Let>vContinue=1

While>vContinue=1
  Let>vWTime=%vWTime%+1
  GoSub>APITimeOut
  IfWindowOpen>Macro Scheduler Message
  Else
    Let>vContinue=0
  EndIf
EndWhile

SRT>APITimeOut
  Timer>vStartTime
  Let>kk=0
  Repeat>kk
    Add>kk,1
    LibFunc>Kernel32,Sleep,Sres,vWTime
  Until>kk=100
  Timer>vEndTime
  Let>vTime=%vEndTime%-%vStartTime%
  SetControlText>Macro Scheduler Message,TMemo,1,Sleep Time = %vWTime% milliseconds%crlf%100 cycles took %vTime% miliseconds
END>APITimeOut

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts