如何在退出之前等待所有批处理文件完成? [英] How to wait all batch files to finish before exiting?

查看:33
本文介绍了如何在退出之前等待所有批处理文件完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个主批处理文件,而不是调用其他 4 个批处理文件,因此我们可以并行运行.

I have a main batch file than calls 4 other batch files so we can run in parallel.

示例:

Main.bat

    start call batch1.bat
    start call batch2.bat
    start call batch3.bat
    start call batch4.bat

    exit

我希望 Main.bat 在所有批处理 1 到批处理 4 停止执行后退出.这样我就可以得到批处理文件的总运行时间.问题是 Main.bat 甚至在 batch1 到 batch4 完成执行之前就退出了.

I want the Main.bat to exit after all the batch1 to batch 4 has stopped executing. In this way, I can get the total run time of the batch file. Problem is Main.bat exits even before batch1 to batch4 finishes executing.

我尝试为每个批处理文件计算 %errorlevel%,但即使 4 个 .bat 文件仍在运行,它始终返回 0.

I tried to compute for %errorlevel% for each batch file, but it always return 0 even though the 4 .bat files are still running.

希望有人能帮助我!

谢谢!:)

推荐答案

我认为这是最简单最有效的方法:

I think this is the simplest and most efficient way:

@echo off

echo %time%

(
    start call batch1.bat
    start call batch2.bat
    start call batch3.bat
    start call batch4.bat
) | set /P "="

echo %time%

在这个方法中,主文件中的等待状态是事件驱动,所以它不消耗任何CPU时间!

In this method the waiting state in the main file is event driven, so it does not consume any CPU time!

编辑:添加了一些解释

set/P 命令将在 ( block ) 中的任何命令输出一行时终止,但 start 命令不会't 在这个 cmd.exe 中显示任何行.这样,set/P 一直等待输入,直到所有由 start 命令启动的进程结束.此时与 ( block ) 关联的管道关闭,因此 set/P Stdin 关闭,set/P 命令是由操作系统终止.

The set /P command would terminate when anyone of the commands in the ( block ) outputs a line, but start commands don't show any line in this cmd.exe. This way, set /P keeps waiting for input until all processes started by start commands ends. At that point the pipe line associated to the ( block ) is closed, so the set /P Stdin is closed and set /P command is terminated by the OS.

这篇关于如何在退出之前等待所有批处理文件完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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