User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,428 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,565 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 3454 | Replies: 7
Reply
Join Date: Aug 2005
Location: Seaham, UK
Posts: 188
Reputation: bops is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 3
bops bops is offline Offline
Junior Poster

Execute command or application without waiting

  #1  
Jun 19th, 2007
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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,541
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 972
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Execute command or application without waiting

  #2  
Jun 19th, 2007
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.
<<Freelance Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: Aug 2005
Posts: 4,832
Reputation: iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light 
Rep Power: 17
Solved Threads: 324
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: Execute command or application without waiting

  #3  
Jun 19th, 2007
You should use the windows api instead of using system.
... the hat of 'is this a cat in a hat?'
Reply With Quote  
Join Date: Jan 2007
Location: Maryland, USA
Posts: 1,036
Reputation: Sturm is on a distinguished road 
Rep Power: 4
Solved Threads: 23
Sturm's Avatar
Sturm Sturm is offline Offline
Veteran Poster

Re: Execute command or application without waiting

  #4  
Jun 19th, 2007
for *nix I think you can postfix your command with &. This will allow system() to return immediately.
Last edited by Sturm : Jun 19th, 2007 at 6:15 pm.
"Hey ass, don't hijack my thread. This is serious." -JoshSCH
Reply With Quote  
Join Date: Aug 2005
Location: Seaham, UK
Posts: 188
Reputation: bops is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 3
bops bops is offline Offline
Junior Poster

Re: Execute command or application without waiting

  #5  
Jun 19th, 2007
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?
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,541
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 972
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Execute command or application without waiting

  #6  
Jun 19th, 2007
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.
<<Freelance Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: Jan 2007
Location: Maryland, USA
Posts: 1,036
Reputation: Sturm is on a distinguished road 
Rep Power: 4
Solved Threads: 23
Sturm's Avatar
Sturm Sturm is offline Offline
Veteran Poster

Re: Execute command or application without waiting

  #7  
Jun 19th, 2007
>I don't think that works inside a c++ program, only from a shell program.

No it works. Just tested it.

  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. system("emacs&");
  8. return 0;
  9. }

starts emacs and returns immediately
Last edited by Sturm : Jun 19th, 2007 at 8:43 pm.
"Hey ass, don't hijack my thread. This is serious." -JoshSCH
Reply With Quote  
Join Date: Feb 2007
Location: Bangalore, India
Posts: 535
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Rep Power: 4
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: Execute command or application without waiting

  #8  
Jun 20th, 2007
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.
  1. pid = fork() ;
  2. if( 0 == pid )
  3. system("...") ;
  4. //your normal code here..
Are you Agile.. ?
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 1:20 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC