2010-09-04 122 views
2

要監控帶寬使用情況,並且不要在啓動時不必要地加載程序,我想執行dumeter.exe然後firefox.exe.When關閉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 

需要的taskkill運行僅當Firefox正在使用一個bat文件,但有時dumeter開始closed.Tried並關閉自身不等待。

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

回答

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?

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 
+0

歡迎堆棧溢出 - 有你真好。請閱讀我如何提出一個好問題? https://stackoverflow.com/help/how-to-ask以及如何創建一個最小化,完整和可驗證的示例,以幫助將堆棧溢出內容保持在最高級別,並增加獲得正確答案的機會。 https://stackoverflow.com/help/mcve – 2017-10-29 19:30:56