简体   繁体   English

等待程序完成

[英]Wait for program to complete

To monitor the bandwidth usage and not to unnecessarily load programs in the start up,I want to execute the dumeter.exe then firefox.exe.When I shutdown firefox it should kill dumeter.I used the following code to start 为了监视带宽使用情况,而不是在启动过程中不必要地加载程序,我想先执行dumeter.exe,然后执行firefox.exe。当我关闭firefox时,它应该杀死dumeter。

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

Need to run taskkill only when firefox is closed.Tried using a bat file but sometimes the dumeter starts and closes on its own does not wait. 仅在关闭firefox时才需要运行taskkill。尝试使用bat文件,但有时dumeter会自行启动和关闭而不会等待。

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

You can wait for a process to end by subscribing to the appropriate WMI event. 您可以通过订阅适当的WMI事件来等待进程结束。 Here's an example: 这是一个例子:

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

More info here: How Can I Start a Process and Then Wait For the Process to End Before Terminating the Script? 此处有更多信息: 如何启动进程,然后等待进程结束,然后再终止脚本?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM