17

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.

2

6 Answers 6

28

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 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

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:

: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
6
  • That's what I'm actually looking for. To execute more commands I need 1) add another start command 2) modify line 1>nul 2>nul 3>nul ping /n 3 3) add new index to for for %%N in (1 2 3) 4) replace 2>nul with 3>nul at the end of loop, am I right?
    – Jaded
    Sep 13, 2013 at 8:06
  • 3
    @Crow 1) and 3) yes. 2) and 4) no. The > character is the redirection operator. 1>nul disables stdout output. 2>nul disables stderr output. They MUST remain the same. The PING command is a hack that introduces a ~1 second delay without consuming CPU resources. It could be replaced by >nul timeout 1 /nobreak, except some older machines don't have that command.
    – dbenham
    Sep 13, 2013 at 11:43
  • 1
    @Crow - I've updated the answer to show how to modify the wait loop so that it does not need to change when new processes are added.
    – dbenham
    Sep 13, 2013 at 12:09
  • Thanks, all this scripting looks like pure magic on the first sight.
    – Jaded
    Sep 13, 2013 at 12:25
  • The poll wait time could be further improved by attempting to delete the lock file within the loop. Before starting concurrent tasks, ensure no lock files exist with DEL "%lock%*". After starting concurrent tasks, poll as follows - :Wait 1>NUL 2>NUL PING /n 2 ::1 FOR %%F IN ("%lock%*") DO ( DEL %%F ) 1>NUL 2>NUL (DIR "%lock%*" && GOTO :Wait) 1>NUL 2>NUL It is important that DIR set ERRORCODE to 1 if all the files have been removed. This appears to be the case on Windows 7 Enterprise.
    – Venky
    Mar 19, 2018 at 18:47
1

Adapting on @dbenham answer, here's how i made it to work inside a for and for unlimited number of processes:

setlocal enabledelayedexpansion
for %%a in (*.*) do start "" 9>"%temp%/wait!random!.lock" /b /low "myprogram.exe" -program_parameters

:wait
ping /n 2 ::1 > nul
echo waiting processes to finish...
2>nul del "%temp%\wait*.lock" > nul
if exist "%temp%\wait*.lock" goto :wait

... more commands ...

Here %random% don't work, cause it's expanded when for is called, therefore making all numbers the same. So I enabledelayedexpansion 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).

2
  • Since it is guaranteed that all of the file names will be unique, use of the RANDOM variable can be omitted. The lock files can be named uniquely with the file name. 9>"%temp%/wait_%~a.lock"
    – lit
    Feb 22, 2018 at 22:19
  • Sorry, I slipped a percent. It should be 9>"%temp%/wait_%%~a.lock"
    – lit
    Feb 23, 2018 at 14:48
0

You are starting several instances of the same app.exe?

  • start them all
  • loop tasklist |find "app.exe" until %errorlevel% is 1
  • continue with your script
1
  • 1
    @jave.web: yes, Every loop without a wait-cycle (may it be sleep, timeout, ping or any other command that needs some time without eating CPUtime) is a performance killer. My answer wasn't meant to be a "ready to use code" (obviously...), but a "how-to".
    – Stephan
    Feb 28, 2015 at 8:43
0

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#.

0

In a separate batch file asynchBatch.bat put:

start app.exe param1
start app.exe param2

Then in caller batch file write:

call asynchBatch.bat
other executions
...

The other executions will start only after both app.exe param1 and app.exe param2 finish.

-1

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:

start /w app.exe param1
start /w app.exe param2
2
  • Seen that. First part - running instances - is ok, but how to process results after ALL of them finish still not very clear for me.
    – Jaded
    Sep 12, 2013 at 8:20
  • 10
    Not applicable. /w will run each instance sequentially (run 1, wait for it to finish, run 2). What I need and I thought it's clear is (run1, run2, wait for them both to finish, do post-operation).
    – Jaded
    Sep 12, 2013 at 8:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.