繁体   English   中英

从批处理文件中,如何在调用taskkill.exe后等待进程退出?

[英]from a batch file, how can I wait for a process to exit, after calling taskkill.exe?

我想编写一个批处理文件,用于更新正在运行的进程(常规应用程序)正在使用的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语句,因此永远不会达到这些行,因此永远不会评估语法。 因此,这些行不是问题。

暂无
暂无

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

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