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.

Recommended Answers

All 7 Replies

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.

Member Avatar for iamthwee

You should use the windows api instead of using system.

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

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?

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.

>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

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..
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.