如何使批处理等待多个子进程 [英] how to make batch wait for multiple subprocesses
问题描述
我有这个批处理文件:
SET WINRAR="C:\Program Files (x86)\WinRAR"
start "" %WINRAR%\WinRAR.exe a -u -m5 "Group 1.rar" "Group 1" "Group 1"
start "" %WINRAR%\WinRAR.exe a -u -m5 "Group 2.rar" "Group 2" "Group 2"
start "" %WINRAR%\WinRAR.exe a -u -m5 "Group 3.rar" "Group 3" "Group 3"
start "" "D:\"
我想要所有的rar进程同时工作,并在rar完成后打开目录 D:\
。
I want all the rar processes to work at the same time and to open the directory D:\
after rar finishes.
推荐答案
您可以通过启动没有/ WAIT的un-rar进程,并检查它们是否使用 tasklist
完成:
You can achieve this by starting your un-rar processes without /WAIT and check whether they're finished using tasklist
:
@ECHO OFF
SET WINRAR="C:\Program Files (x86)\WinRAR"
start "" %WINRAR%\WinRAR.exe a -u -m5 "Group 1.rar" "Group 1" "Group 1"
start "" %WINRAR%\WinRAR.exe a -u -m5 "Group 2.rar" "Group 2" "Group 2"
start "" %WINRAR%\WinRAR.exe a -u -m5 "Group 3.rar" "Group 3" "Group 3"
:LOOP
tasklist /FI "IMAGENAME eq WinRAR.exe" 2>NUL | find /I /N "WinRAR.exe">NUL
if %ERRORLEVEL%==0 (
ping localhost -n 6 >nul
GOTO LOOP
)
start "" "D:\"
代码执行以下操作:
首先,开始所有的un-rar进程。然后检查您的任务列表是否包含任何winrar.exe进程。如果是这样,您等待5秒钟,然后再次检查。一旦任务列表中没有更多的winrar.exe条目,您就可以进入 startD:\
。
First you start all your un-rar processes. Then you check whether your tasklist contains any winrar.exe processes. If it is the case you wait 5 seconds and check again. As soon as there are no more winrar.exe entries in the task list you go on to start "" "D:\"
.
编辑:正如你问什么等待ping工作,这里是解释。
对不起,我的代码中有一个错误。 ping localhost -n 6
,而不是 ping localhost -6
。
As you've asked what how wait by ping works, here is the explanation.
Sorry, there was a bug in my code. It's ping localhost -n 6
and not ping localhost -6
.
ping localhost -n 6> nul
使您的系统ping localhost 6次,每次ping之间1秒。 6之间有1秒的ping是5 :)当localhost在1毫秒内响应,你等待大约5秒。 > nul
会取消您控制台中的ping命令的输出。
ping localhost -n 6>nul
makes your system ping localhost 6 times with 1 second between each ping. 6 pings with 1 second between is 5 :) As localhost responds within 1 ms you are waiting about 5 seconds. >nul
suprsses the output of the ping command in your console.
这篇关于如何使批处理等待多个子进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!