How to set delay in vbscript

前端 未结 11 1407
鱼传尺愫
鱼传尺愫 2020-12-03 09:35

How to set delay in vbscript?

WScript.Sleep(100) does not work on Windows XP, Vista.

11条回答
  •  既然无缘
    2020-12-03 10:07

    Here is an update to the solution provided by @user235218 that allows you to specify number of milliseconds you require.

    Note: The -n option is the number of retries and the -w is the timeout in milliseconds for ping. I chose the 127.255.255.254 address because it is in the loopback range and ms windows doesn’t respond to it.

    I also doubt this will provide millisecond accuracy but on another note i tried it in an application using the ms script control and whilst the built in sleep function locked up the interface this method didn't.

    If somebody can provide an explanation for why this method didn't lock up the interface we could make this answer more complete. Both sleep functions where run in the user thread.

    Const WshHide = 0
    Const WAIT_ON_RETURN = True
    
    Sub Sleep(ByVal ms)
    
       Dim shell 'As WScript.Shell
    
       If Not IsNumeric(ms) Then _
           Exit Sub
    
       Set shell = CreateObject("WScript.Shell")
    
       Call shell.Run("%COMSPEC% /c ping -n 1 -w " & ms & " 127.255.255.254 > nul", WshHide, WAIT_ON_RETURN)
    
    End Sub

提交回复
热议问题