2

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

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.

 WshShell.Run "taskkill /f /im dumeter.exe"  
 Set WshShell = Nothing
Dario Dias
  • 817
  • 6
  • 19
  • 32

2 Answers2

4

You can wait for a process to end by subscribing to the appropriate WMI event. 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?

Helen
  • 87,344
  • 17
  • 243
  • 314
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
Brian
  • 1
  • Welcome to Stack Overflow - nice to have you. Please read How do I ask a good question? https://stackoverflow.com/help/how-to-ask and How to create a Minimal, Complete, and Verifiable example to help keeping Stack Overflows content on the highest possible level and increase your chances getting an appropriate answer. https://stackoverflow.com/help/mcve –  Oct 29 '17 at 19:30