如何等待进程终止以执行批处理文件中的另一个进程 [英] How to wait for a process to terminate to execute another process in batch file

查看:21
本文介绍了如何等待进程终止以执行批处理文件中的另一个进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在批处理文件中执行另一个进程之前等待一个进程终止?假设我有一个进程 notepad.exe,我需要在执行 wordpad.exe 之前杀死它.然后,当 wordpad.exe 终止时,我需要再次启动 notepad.exe.我该怎么做?

How to wait for a process to terminate before executing another process in a batch file? Let's say I have a process notepad.exe that I need to kill before executing wordpad.exe. Then, when wordpad.exe is terminated, I need to launch notepad.exe again. How do I do that?

推荐答案

试试这样...

@ECHO OFF

PSKILL NOTEPAD

START "" "C:Program FilesWindows NTAccessorieswordpad.exe"

:LOOP
PSLIST wordpad >nul 2>&1
IF ERRORLEVEL 1 (
  GOTO CONTINUE
) ELSE (
  ECHO Wordpad is still running
  TIMEOUT /T 5
  GOTO LOOP
)

:CONTINUE
NOTEPAD

我使用了 PSLISTPSEXEC,但你也可以使用 TASKKILLTASKLIST.>nul 2>&1 只是为了隐藏 PSLIST 的所有输出.SLEEP 5 行不是必需的,只是用来限制您检查写字板是否仍在运行的频率.

I used PSLIST and PSEXEC, but you could also use TASKKILL and TASKLIST. The >nul 2>&1 is just there to hide all the output from PSLIST. The SLEEP 5 line is not required, but is just there to restrict how often you check if WordPad is still running.

这篇关于如何等待进程终止以执行批处理文件中的另一个进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆