等待程序完成

时间:2010-09-04 06:30:34

标签: vbscript

要监视带宽使用情况而不是在启动时不必要地加载程序,我想执行dumeter.exe然后执行firefox.exe。当我关闭firefox它应该杀死dumeter.I使用以下代码启动

Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "c:\progra~1\dumeter\dumeter.exe"
WshShell.Run "c:\progra~1\mozill~1\firefox.exe

只有当firefox关闭时才需要运行taskkill。使用bat文件但有时dumeter自行启动和关闭不会等待。

 WshShell.Run "taskkill /f /im dumeter.exe"  
 Set WshShell = Nothing

2 个答案:

答案 0 :(得分:4)

您可以通过订阅相应的WMI事件来等待进程结束。这是一个例子:

strComputer = "."
Set oWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

''# Create an event query to be notified within 5 seconds when Firefox is closed
Set colEvents = oWMI.ExecNotificationQuery _
    ("SELECT * FROM __InstanceDeletionEvent WITHIN 5 " _
     & "WHERE TargetInstance ISA 'Win32_Process' " _
     & "AND TargetInstance.Name = 'firefox.exe'")

''# Wait until Firefox is closed
Set oEvent = colEvents.NextEvent

此处有更多信息:How Can I Start a Process and Then Wait For the Process to End Before Terminating the Script?

答案 1 :(得分:0)

Option Explicit

Const PROC_NAME = "<Process_You_Want_to_Check>"
Const SLEEP_INTERVAL_MS = 5000 '5 secs

Dim objWMIService
Dim colProcesses, objProcess, inteproc

Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")

inteproc = -1 'set in unknown state

Do Until inteproc = 0

Set colProcesses = objWMIService.ExecQuery(_
    "Select * from Win32_Process where Name='" & PROC_NAME & "'")
    inteproc = colProcesses.count

If inteproc > 0 then
WSCRIPT.ECHO "Process " & PROC_NAME & " is still runing, wait for " & SLEEP_INTERVAL_MS / 1000 & " seconds"
WScript.Sleep(SLEEP_INTERVAL_MS)

else
    wscript.echo "Process " & PROC_NAME & " Finished. Continue running scripts"

End If

Loop