2010-09-04 5 views
2

Pour surveiller l'utilisation de la bande passante et ne pas inutilement charger les programmes au démarrage, je veux exécuter le dumeter.exe puis firefox.exe. Quand j'arrêterai Firefox, il devrait tuer dumeter.J'ai utilisé ce qui suit code pour commencerAttendez que le programme se termine

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

besoin d'exécuter taskkill que lorsque Firefox est closed.Tried à l'aide d'un fichier de chauve-souris, mais parfois le début de dumeter et ferme sur son propre n'attend pas.

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

Répondre

4

Vous pouvez attendre la fin d'un processus en vous abonnant à l'événement WMI approprié. Voici un exemple:

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 

Plus d'infos ici: 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

Bienvenue sur Stack Overflow - agréable de vous. S'il vous plaît lire Comment puis-je poser une bonne question? https://stackoverflow.com/help/how-to-ask et Comment créer un exemple minimal, complet et vérifiable pour aider à maintenir le contenu de Stack Overflows au niveau le plus élevé possible et augmenter vos chances d'obtenir une réponse appropriée. https://stackoverflow.com/help/mcve –

Questions connexes