8

MyFile1.bat invokes MyFile2.bat twice:

start MyFile2.bat argA, argB, argC
start MyFile2.bat argX, argY, argZ

At this point, how can I wait until both processes spawned by the calls to MyFile2.bat have completed?

Roman C
  • 49,761
  • 33
  • 66
  • 176
barak manos
  • 29,648
  • 10
  • 62
  • 114

4 Answers4

7

Simple use the Start /WAIT parameter.

start /wait MyFile2.bat argA, argB, argC
start /wait MyFile2.bat argX, argY, argZ
Knuckle-Dragger
  • 6,644
  • 4
  • 26
  • 41
  • 2
    Or change `start` to `call`, which was my first thought. However, perhaps they want the two calls to run in parallel. In this case, neither your suggestion nor mine would really be appropriate. – Andriy M Dec 14 '13 at 01:18
3
start /w cmd /c "start cmd /c MyFile2.bat argA, argB, argC & start cmd /c MyFile2.bat argA, argB, argCt"

According to my tests this should work providing that the MyFile2.bat .Eventually the full paths to the bat files should be used.

npocmaka
  • 55,367
  • 18
  • 148
  • 187
3

You may use "status files" to know that; for example, in MyFile1.bat do the following:

echo X > activeProcess.argA
start MyFile2.bat argA, argB, argC
echo X > activeProcess.argX
start MyFile2.bat argX, argY, argZ
:waitForSpawned
if exist activeProcess.* goto waitForSpawned

And insert this line at end of MyFile2.bat:

del activeProcess.%1

You may also insert a ping delay in the waiting cycle in order to waste less CPU in this loop.

Aacini
  • 65,180
  • 12
  • 72
  • 108
  • +1, but to make sure there can be no issues with permissions, perhaps it would be a good idea to create the flag files in the `%TEMP%` directory. And I would probably add a small delay between `:waitForSpawned` and the condition, and also I would insert the `del` line (did you mean `del activeProcess.*`, by the way?) at the *beginning* of the script, just to be on the safe side in case the script gets interrupted unexpectedly. – Andriy M Dec 14 '13 at 01:26
2

You can do it like this :

start MyFile2.bat argA, argB, argC
start MyFile2.bat argX, argY, argZ ^& echo.^>End.val ^& exit

:testEnd
if exist end.val (del end.val
                  echo Process completed
                  pause)
>nul PING localhost -n 2 -w 1000
goto:testEnd

When the second start2.bat finish is job then a file "End.val" will be created, you just have to test if this file exist, then you know that your process is complete.

If the first myfile2 can take more time to execute then the second you can do the same (with another filename) with the first start myfile2.bat and make a test more in the :testend

 start MyFile2.bat argA, argB, argC ^& echo.^>End1.val ^& exit
 start MyFile2.bat argX, argY, argZ ^& echo.^>End.val ^& exit

:testEnd
if exist end.val if exist end1.val (del end.val
                                    del end1.val
                                    echo Process completed
                                    pause)
>nul PING localhost -n 2 -w 1000
goto:testEnd
SachaDee
  • 9,245
  • 3
  • 23
  • 33