execute multiple commands at the same time?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
©opy[it]®ight
Posts: 60
Joined: 17 Mar 2012 09:59

execute multiple commands at the same time?

#1 Post by ©opy[it]®ight » 13 Aug 2012 13:02

Hello everyone,

I'm working on a script that does a couple of tasks, but i would like to run those commands simultaneously.

What i've tried so far is the following:

Code: Select all

START "" %TASK1%
START "" %TASK2%
START "" %TASK3%


Now, this seems to work and all but i also found that you can achieve the same thing using the following code:

Code: Select all

CMD /C %TASK1%
CMD /C %TASK2%
CMD /C %TASK3%


And i've even found examples where the following combination was used:

Code: Select all

START "" CMD /C <some command>


Now my question: Which of the above methods is prefered?
I would like to hear it from you ;)

/©opy[it]®ight

Dos_Probie
Posts: 233
Joined: 21 Nov 2010 08:07
Location: At My Computer

Re: execute multiple commands at the same time?

#2 Post by Dos_Probie » 13 Aug 2012 15:14

It really depends on how you are running your tasks or applications..
example: If running applications from RunOnceEx then you would need
to use the cmd /c .. but if doing tasks on post installs then the Start ""
would be fine .http://gosh.msfn.org/using_runonceex.htm
Of course you want to test and retest to ensure that it works the way
you want it..

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: execute multiple commands at the same time?

#3 Post by aGerman » 13 Aug 2012 15:27

If "at the same time" means that you want them to run asynchronously then you have to use the START command. Whether or not you should run it in another cmd process depends on what exactly you want to execute. In case of separate batch files use
START "" CMD /C ...

Regards
aGerman

©opy[it]®ight
Posts: 60
Joined: 17 Mar 2012 09:59

Re: execute multiple commands at the same time?

#4 Post by ©opy[it]®ight » 13 Aug 2012 15:56

Thanks for the answers :-)

So (if i got it right), there's a difference between running programs
using START "" , using CMD /C and using START "" CMD /C?

I initially thought that START executes a command interpreter whereas CMD /C is one, on itself.
I'm basically trying to find out in what way the above variations differ from each other.

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: execute multiple commands at the same time?

#5 Post by aGerman » 13 Aug 2012 16:38

START runs asynchronously (the batch file executes the next line immediately).
CMD /C runs synchronously (the batch file does wait until the called command was finished).
The reason why I recommend you to combine both in case of executing other batch files is that these windows don't close normally otherwise.

Regards
aGerman

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: execute multiple commands at the same time?

#6 Post by Liviu » 13 Aug 2012 16:43

©opy[it]®ight wrote:So (if i got it right), there's a difference between running programs using START "" , using CMD /C and using START "" CMD /C?

I initially thought that START executes a command interpreter whereas CMD /C is one, on itself.

First difference is that START can also launch non-executable files by association, for example "start http://www.google.com".

This is an attempt to summarize some of the other differences, note that some depend on whether the target is a console program vs. a GUI.

Code: Select all

                         runs inside     new instance        runs
                       parent console   of cmd.exe (*)   asynchronously

do-pause.cmd                yes              no               no
cmd /c do-pause.cmd         yes             yes               no
start /wait do-pause.cmd     no             yes               no
start do-pause.cmd [1]       no             yes              yes

notepad.exe [2]             n/a              no              yes
start /wait notepad.exe     n/a              no               no


(*) when running inside a new instance of cmd.exe
 -  changes to environment variables are not effected back in the original environment
 -  calling "exit" in a batch closes the new instance, not the original one

[1] same as "start cmd /k do-pause.cmd"
[2] same as "start notepad.exe"
You can test the above by creating a simple batch file do-pause.cmd containing just:

Code: Select all

@pause
@exit

Liviu

©opy[it]®ight
Posts: 60
Joined: 17 Mar 2012 09:59

Re: execute multiple commands at the same time?

#7 Post by ©opy[it]®ight » 13 Aug 2012 18:02

Oh wauw. aGerman and Liviu just nailed it.

Thank you so much for the invaluable feedback 8)

batch closed :mrgreen:

Post Reply