等待多个应用程序从批处理文件异步运行完成 [英] Wait for multiple applications run asynchronously from batch file to finish

查看:250
本文介绍了等待多个应用程序从批处理文件异步运行完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有是运行多个应用程序实例一个简单的Windows批处理文件:

There is a simple Windows batch file that runs multiple instances of application:

start app.exe param1
start app.exe param2

有没有一种办法,同时异步运行它们(这上面那样),等待他们俩到完成执行其他操作 - 像C#

Is there a way to run them asynchronously at the same time (which above does) and wait for them both to finish to perform other actions - something like C#

Task.WhenAll(tasksList.ToArray());
/* Process tasksList.Result */


/等待开关不会在这里帮忙,有的轮询如果特定实例仍可能运行。

?
/wait switch will not help here, some polling for if particular instance is still running maybe.

推荐答案

我想这个问题是不是略有不同等待并行批处理脚本在的,这个应用程序正在等待的.exe程序,而不是完成批处理脚本的。但该溶液几乎是相同的。

I suppose this question is slightly different than Waiting for parallel batch scripts in that this application is waiting for .exe processes to finish instead of batch scripts. But the solution is nearly identical.

您必须实例某种形式的投票在你的主批处理脚本。您可以有效地创建通过重定向锁定文件。锁定文件保持锁定状态,直到进程终止。您的批处理脚本民意调查,检查它是否能打开所有锁文件。一旦成功,它知道所有的进程已经结束。

You must instantiate some form of polling in your master batch script. You can effectively create a lock file via redirection. The lock file remains locked until the process terminates. Your batch script polls, checking if it can open all the lock files. Once it succeeds, it knows all the processes have ended.

在下面的解决方案的唯一显著不同的是,开始启动该.exe文件,而不是直接通过 CMD / C 。我还了解到,(呼叫)是有效地执行无操作总是成功的一个非常快速的方式。所以我取代的(呼叫)代替 REM

The only significant difference in the solution below is that START launches the .exe directly instead of launching a batch through CMD /C. I also learned that (call ) is an extremely fast way to effectively perform a no-op that always succeeds. So I substitued (call ) in place of rem

@echo off
setlocal
set "lock=%temp%\wait%random%.lock"

:: Launch processes asynchronously, with stream 9 redirected to a lock file.
:: The lock file will remain locked until the script ends.
start "" 9>"%lock%1" notepad.exe
start "" 9>"%lock%2" notepad.exe

:Wait for both processes to finish (wait until lock files are no longer locked)
1>nul 2>nul ping /n 2 ::1
for %%N in (1 2) do (
  (call ) 9>"%lock%%%N" || goto :Wait
) 2>nul

::delete the lock files
del "%lock%*"

:: Finish up
echo Done - ready to continue processing

请参阅壳进程并行执行中的锁技术的pretty复杂的应用程序,调节的最大数量的并行处理,通过 PSEXEC 引导进程特定CPU或机器的能力。这个问题的答案还提供了锁定文件的技术是如何工作的一个更全面的解释。

See Parallel execution of shell processes for a pretty sophisticated application of the lock technique that regulates the maximum number of parallel processes, with the ability to direct processes to specific CPUs or machines via PSEXEC. That answer also provides a fuller explanation of exactly how the lock file technique works.

修改

的等待循环可以被修改,使得它并不需要改变在添加更多的进程:

The wait loop can be modified so that it does not need to change as you add more processes:

:Wait for all processes to finish (wait until lock files are no longer locked)
1>nul 2>nul ping /n 2 ::1
for %%F in ("%lock%*") do (
  (call ) 9>"%%F" || goto :Wait
) 2>nul

这篇关于等待多个应用程序从批处理文件异步运行完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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