Windows命令行在下一个命令之前等待任务结束

时间:2016-12-18 19:40:38

标签: windows cmd command wait exit

我有一些Windows软件在退出时打开一个网站。我想在退出example.exe后运行一个关闭浏览器的批处理

example.exe 

(等待example.exe结束)

taskkill /im firefox.exe
taskkill /im iexplore.exe
taskkill /im chrome.exe

1 个答案:

答案 0 :(得分:0)

您正在观察的问题可能是在游戏结束时,浏览器将作为另一个进程启动。在浏览器实际启动之前可能需要一段时间,但游戏不会等待而只是退出。因此,当您使用taskkill命令时,浏览器仍未打开。您需要的是验证浏览器进程是否已经启动的方法,而不是确定exemple.exe(或您的游戏)是否已退出的方法。你可以这样做:

 :waitOnBrowser
 tasklist | findstr /B /C:"chrome.exe" /C:"firefox.exe" /C:"iexplore.exe" > nul
 REM the errorlevel will only be 0 if one of the browser has been found in the tasklist
 IF ERRORLEVEL 1 goto :waitOnBrowser

 REM At this point, you're sure at least one browser has been started
 REM taskkill can come here

IF ERRORLEVEL 1IF %ERRORLEVEL% GTR 1有点相同(参见IF in batch)。