5

How can I delay a Vb Script? The following codes did not work for me:

wscript.sleep 1000

Delay 10

Sub Delay( seconds )
    Dim wshShell, strCmd
    Set wshShell = CreateObject( "Wscript.Shell" )
    strCmd = "%COMSPEC% /C (PING -n " & ( seconds + 1 ) & " 127.0.0.1 >NUL 2>&1 || PING -n " & seconds & " ::1 >NUL 2>&1)"
    wshShell.Run strCmd, 0, 1
    Set wshShell = Nothing
End Sub

dim oshell 
set oshell = CreateObject("Wscript.Shell") 


obshell.run "%windir%\system32\rundll32.exe kernel32.dll Sleep 5000" 
ladiesman1792
  • 243
  • 2
  • 5
  • 21

2 Answers2

4

The first statement, WScript.Sleep 1000, does work. Your error must be somewhere else.

Proof

Create a file test.vbs on your desktop:

WScript.Echo Time()
WScript.Sleep 2000
WScript.Echo Time()

Run as follows:

C:\Users\...\Desktop>cscript test.vbs
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.

11:31:34
11:31:36

Note the two second difference. (If this exact script produces a different outcome on your PC, please say so in the comments and accept my apology.)

Heinzi
  • 167,459
  • 57
  • 363
  • 519
  • The WScript tag seems to be not working to my project. I am using Visual Studio 2010 Setup Project in running my scripts. – ladiesman1792 Jan 22 '15 at 10:37
  • Here is my script, http://justpaste.it/j0y7. The code doesnt work if I put WSCRIPT.Sleep. I put a message box instead to pass the time. – ladiesman1792 Jan 22 '15 at 10:42
  • 4
    @ladiesman1792: Ah, OK, that's a significant piece of information. :-) Apparently, you cannot use WScript in Setup Projects, not even with CreateObject. You can either [use one of the workarounds discussed here](http://stackoverflow.com/q/8487935/87698) or [use another method of waiting (e.g. busy waiting in a loop) discussed here](http://stackoverflow.com/q/1729075/87698). – Heinzi Jan 22 '15 at 10:42
  • 1
    Thank you! This one worked for me: `Dim dteWait dteWait = DateAdd("s", 10, Now()) Do Until (Now() > dteWait) Loop` – ladiesman1792 Jan 22 '15 at 10:49
  • One of the problems with a hard CPU loop like that is the potential monopolization of the system. On multicore systems it shouldn't be too bad, but watch out for it. – PhilDW Jan 23 '15 at 18:24
1

You cannot use the WScript object in VB script custom actions. The WScript object is supplied by the script environment when you run it in Windows Script Host, and is not in the MSI environment. That means there is no way to do a delay, so maybe you could describe the problem you're having that the delay might solve.

PhilDW
  • 20,260
  • 1
  • 18
  • 28