There is a simple Windows batch file that runs multiple instances of application:
start app.exe param1
start app.exe param2
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.
Powershell has more sophisticated ways to do it without wait loops. You can wrap or call your scripts with powershell and even implement your preferred managed solutions for doing it exactly as you do it in C#.
Basically, you're doing it right using the START command becuase it is inteded for what you need now.
In addition, I would suggest you to refering to this great answer: https://stackoverflow.com/a/1449192/952310
UPDATE:
For waiting each program to finish, use the /W switch, look:
In a separate batch file
asynchBatch.bat
put:Then in caller batch file write:
The other executions will start only after both app.exe param1 and app.exe param2 finish.
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.
The only significant difference in the solution below is that
START
launches the .exe directly instead of launching a batch throughCMD /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 ofrem
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.EDIT
The wait loop can be modified so that it does not need to change as you add more processes:
Adapting on @dbenham answer, here's how i made it to work inside a
for
and for unlimited number of processes:Here
%random%
don't work, cause it's expanded whenfor
is called, therefore making all numbers the same. So Ienabledelayedexpansion
and use!random!
.On the loop I try to delete all files.
2>nul
will make sure any error won't show on screen. It will only go through when all can be deleted (if exist
).You are starting several instances of the same app.exe?
tasklist |find "app.exe"
until%errorlevel%
is1