当前位置:首页> 正文

关于vbscript:WScript.Shell和阻止执行?

关于vbscript:WScript.Shell和阻止执行?

WScript.Shell and blocking execution?

我正在使用WScript通过使用WScript.Shell调用外部程序来自动执行某些任务。

但是,现在它不等待外部程序完成,而是继续运行。这会引起问题,因为我有一些任务要先完成,而其他任务则要先完成。

我正在使用类似以下的代码:

1
2
3
4
ZipCommand ="7za.exe a -r -y" & ZipDest & BuildLabel &".zip" & buildSourceDir

Set wshShell = WScript.CreateObject("Wscript.Shell")
wshShell.run ZipCommand

有没有办法做到这一点,使其阻塞,直到shell执行的程序返回为止?


结果是,虽然循环是严重的CPU吞噬:P

我找到了更好的方法:

1
2
3
4
5
ZipCommand ="7za.exe a -r -y" & ZipDest & BuildLabel &".zip" & buildSourceDir

Set wshShell = WScript.CreateObject("Wscript.Shell")

wshShell.Run ZipCommand,1,1

最后两个参数是"显示窗口"和"块执行:"


如果使用" Exec"方法,它将返回一个引用,因此可以轮询" Status"属性来确定它何时完成。这是来自msdn的示例:

1
2
3
4
5
6
7
8
Dim WshShell, oExec
Set WshShell = CreateObject("WScript.Shell")

Set oExec = WshShell.Exec(ZipCommand)

Do While oExec.Status = 0
    WScript.Sleep 100
Loop

展开全文阅读

相关内容