Executing multiple System() commands

P

Paul Policarp

I'm attempting to make an application that will call and execute multiple
"system()" cmd commands simultaneously. The problem is that when the program
hits the first system() it waits for it to finish executing (and it can run
indefinitely until dismissed with Ctrl+C) and only after it has closed does
it move on to the next.

Is there any way to make the program move on immediately after calling
system(), without waiting for a return value?
 
D

David Wilkinson

Paul said:
I'm attempting to make an application that will call and execute multiple
"system()" cmd commands simultaneously. The problem is that when the program
hits the first system() it waits for it to finish executing (and it can run
indefinitely until dismissed with Ctrl+C) and only after it has closed does
it move on to the next.

Is there any way to make the program move on immediately after calling
system(), without waiting for a return value?

Paul:

ShellExecute(), ShellExecuteEx().
 
P

Paul Policarp

Thank you, but sadly, that doesn't help me a lot. My command is in the form
of a string and it's not a standard Windows command. It's custom made
depending on certain parameters the application sets. Is there any function
that runs commands in a separate process but takes a single string as an
argument?
 
D

David Wilkinson

Paul said:
Thank you, but sadly, that doesn't help me a lot. My command is in the form
of a string and it's not a standard Windows command. It's custom made
depending on certain parameters the application sets. Is there any function
that runs commands in a separate process but takes a single string as an
argument?

Paul:

Well, I'm not quite sure what you mean, but you could make these system() calls
from worker threads in your application.
 
P

Paul Policarp

I apologize, my phrasing tends to be confusing when I'm in a hurry. Combine
that with not being a native English speaker and there you have it. I'll
attempt to articulate my predicament more accurately.

I am building an application. Based on the state of checkboxes, radio
buttons, edit boxes and IP controls, the application will compose a "command"
string that varies parameter-wise every time the application is run. This
string changes during the execution of the program from the beginning to the
end and a separate process needs to be launched every time the command
changes.

system(command); doesn't do that for me and I haven't been able to make
ShellExecute() do it properly either. I did ponder launching in different
threads but I'm a novice programmer and I'm not good at concurrent
programming.

Is there another solution? All I need is several processes being launched
simultaneously in parallel to my parent program.

Thank you.
 
S

SvenC

Hi Paul,
I haven't been able to
make ShellExecute() do it properly either. I did ponder launching in
different threads but I'm a novice programmer and I'm not good at
concurrent programming.

ShellExecute(0, NULL, _T("yourExecuteableWithPath"), _T("your command line
options"), NULL, SW_SHOWNORMAL);

So all you have to do is split up your command line "c:\myProg\prog.exe
hello world" into "c:\myProg\prog.exe" and "hello world"

ShellExecute does not wait for the command to finish so you can spawn as
many commands in parallel as you want or your computer can cope with.
 
P

Pavel A.

Paul Policarp said:
I'm attempting to make an application that will call and execute multiple
"system()" cmd commands simultaneously. The problem is that when the
program
hits the first system() it waits for it to finish executing (and it can
run
indefinitely until dismissed with Ctrl+C) and only after it has closed
does
it move on to the next.

Is there any way to make the program move on immediately after calling
system(), without waiting for a return value?

Maybe what you need is "start" command (see the Windows help about it).
By default it does not wait.

For example, change system("foo.exe") to change system("start foo.exe")

Regards,
--PA
 
B

Ben Voigt [C++ MVP]

Pavel said:
Maybe what you need is "start" command (see the Windows help about
it). By default it does not wait.

For example, change system("foo.exe") to change system("start
foo.exe")

'start' is a cmd.exe built-in, not a command, so you can't use it with the
system() function.
 
L

Luc E. Mistiaen

no of course but you can do (if you are lazy) :

system ("cmd /c start foo")

/LM
 
P

Pavel A.

Nope, I'm not that lazy :) Quite often actually test thing before posting.
Function system() internally runs the command via cmd.exe (or whatever
%comspec% resolves to).
so system( "start notepad") will work as expected.


Regards,
--PA
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top