同时执行多个批处理文件并监视其处理是否完成 [英] Execute multiple batch files concurrently and monitor if their process is completed

查看:121
本文介绍了同时执行多个批处理文件并监视其处理是否完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个主批处理文件,它调用了多个批处理文件.我希望能够同时执行所有这些批处理文件.完成所有步骤后,我需要在主比对文件中进行进一步的处理.

I have a main batch file which calls multiple batch files. I want to be able to execute all these batch files at the same time. Once they are all done, I have further processes that needs to carry on in the main match file.

当我使用开始"调用多个批处理文件时,能够同时启动所有批处理文件,但是我无法跟踪它们. (主批处理文件认为它们的处理是在执行其他批处理文件的那一刻完成的.)

When I use 'Start' to call the multiple batch files, able to kick off all batch files concurrently but I lose tracking them. (Main batch file thinks their processes are done the moment it executes other batch files).

当我使用呼叫"时,能够监视批处理文件的过程,但是它会按顺序而不是同时启动批处理文件.

When I use 'Call', able to monitor the batch file process, but it kicks off the batch files sequentially instead of concurrently.

有没有解决的办法?在此PC上具有有限的权限,并尝试仅使用批处理来完成此操作.

Is there a way around this? Have limited permissions on this PC and trying to accomplish this using Batch only.

主要批处理文件

call first.bat
call second.bat
call third.bat
:: echo only after all batch process done
echo done!

first.bat

first.bat

timeout /t 10

second.bat

second.bat

timeout /t 10

third.bat

third.bat

timeout /t 10

推荐答案

这是解决此问题的最简单,最有效的方法:

This is the simplest and most efficient way to solve this problem:

(
start first.bat
start second.bat
start third.bat
) | pause

echo done!

在这种方法中,主文件中的等待状态是事件驱动的,因此它不占用任何CPU时间.当( block )中的任何命令输出字符时,pause命令将终止,但是start命令在此cmd.exe 中未显示任何输出.这样,pause会一直等待字符,直到由start命令启动的所有进程结束.此时,与( block )关联的管道已关闭,因此pause Stdin也已关闭,命令由cmd.exe终止.

In this method the waiting state in the main file is event driven, so it does not consume any CPU time. The pause command would terminate when anyone of the commands in the ( block ) outputs a character, but start commands don't show any output in this cmd.exe. In this way, pause keeps waiting for a char until all processes started by start commands ends. At that point the pipe line associated to the ( block ) is closed, so the pause Stdin is closed and the command is terminated by cmd.exe.

这篇关于同时执行多个批处理文件并监视其处理是否完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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