从一个batch file,我怎么能等待一个进程退出后,调用taskkill.exe?

我想编写一个batch file来更新正在运行的进程正在使用的DLL。

要做到这一点,计划是停止进程,将DLL复制到所需的位置,然后重新启动进程。

我知道我可以尝试用taskkill杀死一个进程。 我怎么能确定这个过程在我开枪之后倒下去了?

这是我用过的。 这是批处理文件中的一个子例程。

 set tasklist=%windir%\System32\tasklist.exe set taskkill=%windir%\System32\taskkill.exe ------------------------------------------------------- :STOPPROC set wasStopped=0 set procFound=0 set notFound_result=ERROR: set procName=%1 for /f "usebackq" %%A in (`%taskkill% /IM %procName%`) do ( if NOT %%A==%notFound_result% (set procFound=1) ) if %procFound%==0 ( echo The process was not running. goto :EOF ) set wasStopped=1 set ignore_result=INFO: :CHECKDEAD "%windir%\system32\timeout.exe" 3 /NOBREAK for /f "usebackq" %%A in (`%tasklist% /nh /fi "imagename eq %procName%"`) do ( if not %%A==%ignore_result% (goto :CHECKDEAD) ) goto :EOF ------------------------------------------------------- 

要从批处理文件中使用它,请执行如下操作:

  call :STOPPROC notepad.exe 

完整的例子:

 set tasklist=%windir%\System32\tasklist.exe set taskkill=%windir%\System32\taskkill.exe ------------------------------------------------------- :STOPPROC set wasStopped=0 set procFound=0 set notFound_result=ERROR: set procName=%1 for /f "usebackq" %%A in (`%taskkill% /IM %procName%`) do ( if NOT %%A==%notFound_result% (set procFound=1) ) if %procFound%==0 ( echo The process was not running. goto :EOF ) set wasStopped=1 set ignore_result=INFO: :CHECKDEAD "%windir%\system32\timeout.exe" 3 /NOBREAK for /f "usebackq" %%A in (`%tasklist% /nh /fi "imagename eq %procName%"`) do ( if not %%A==%ignore_result% (goto :CHECKDEAD) ) goto :EOF ------------------------------------------------------- :MAIN call :STOPPROC notepad.exe call :STOPPROC Skype.exe 

你会注意到所有的破折号 – 这当然不是一个批处理文件的合法语法。 但是,由于使用了GOTO语句,这些行从来没有达到过,因此语法从不被评估。 所以这些线路不是问题。