簡體   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