Start multiple tasks in parallel and wait for them in windows?

前端 未结 2 1837
小鲜肉
小鲜肉 2020-11-29 13:10

How do I execute some tasks in parallel in batch script and wait for them?

command1;

# command3, command4 and command5 should execute in sequence say task1
         


        
相关标签:
2条回答
  • 2020-11-29 13:17

    Here's an example how it can be done with tasklist and process window titles:

    launcher.cmd

    @echo off
    for /l %%a in (1,1,5) do start "worker%%a" cmd /c worker.cmd & timeout /t 1 >nul
    :loop
    timeout /t 2 >nul
    tasklist /v /fi "imagename eq cmd.exe" /fo csv | findstr /i "worker" >nul && goto :loop
    echo Workers finished
    

    worker.cmd

    @echo off
    set /a wait=2 + ( %RANDOM% %% 5 ) 
    echo waiting for %wait%...
    timeout /t %wait% >nul
    echo I'm done!
    timeout /t 2 >nul
    

    I'm using tasklist /v /fo csv to get titles.

    0 讨论(0)
  • 2020-11-29 13:26

    I think this is the simplest way to do that:

    command1
    
    (
       start "task1" cmd /C "command3 & command4 & command5"
       start "task2" cmd /C "command6 & command7 & command8"
    ) | pause
    
    command9
    

    Further details in the comments below this answer.

    0 讨论(0)
提交回复
热议问题