0

How can I execute a command like for example using system(); but execute so the program does not need to wait until the function returns before it continues.

Also when executing a command using system(); can you make it so that the console window does not pop up onto the screen.

Thanks.

5
Contributors
7
Replies
8
Views
10 Years
Discussion Span
Last Post by thekashyap
0

You can't do any of those things with system() function. You need to use more os-specific api calls. MS-Windows use CreateProcess(), don't know about *nix.

0

for *nix I think you can postfix your command with &. This will allow system() to return immediately.

0

Oh right, so when using CreateProcess(), how would commands be executed as if it was done by system()? would I call cmd.exe and then specify the command as a parameter to cmd.exe or something along those lines?

0

No you don't run cmd.exe at all, read about the parameters at MSDN here -- the second-to-last parameter has an option to run the process without a window. And CreateProcess() will run without waiting for the process to finish.

>>for *nix I think you can postfix your command with &.
I don't think that works inside a c++ program, only from a shell program.

0

>I don't think that works inside a c++ program, only from a shell program.

No it works. Just tested it.

#include<iostream>

using namespace std;

int main()
{
       system("emacs&");
       return 0;
}

starts emacs and returns immediately

0

If you don't wanna be platform dependent you can still do this by forking a thread for system(). Should be simple enough as well.

pid = fork() ;
if( 0 == pid )
     system("...") ;
//your normal code here..
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.