Click here to Skip to main content
15,936,171 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here's one for everyone, I'm running a batch file within my main c++ program. From what I understand is that the batch becomes a child that might hangup the main program untill it completes. Now which method should I use that would allow the main program to continue along it's merry way while the batch is processing. Also the main program might and probably will create new child instances while the first child is running! Any thoughts welcome! This is Windows.
Updated 26-Sep-11 6:27am
mbue 26-Sep-11 12:57pm    
"hangup the main program untill it completes" who told you that? ShellExecute is completely ansync. You can obtain a process handle to wait for, if you use ShellExecuteEx.
Member 7766180 26-Sep-11 13:03pm    
I'm going by what I read here. Am I misinterputing?
mbue 26-Sep-11 14:06pm    
Nice ;). ShellExecute is definitely async. And the others have flags to control the execution (i.e. P_WAIT or P_NOWAIT). Hope that helps.
Member 7766180 26-Sep-11 14:44pm    
Thank you. I will use ShellExecute.
Chuck O'Toole 26-Sep-11 14:06pm    
in Unix / Linux, system() is a blocking call until the task completes. The Windows RTL implementation of the Unix call does the same thing. Native Windows calls (ShellExecute, ...) are asynch. He's talking about the Unix call.

1 solution

Ok you want a suggestion:
1. Use ShellExecute to start another program from inside yours with asynchronous behauviour. This can be a 32- or 64- executable.
2. If you want to wait for the execution is finished, you should use ShellExecuteEx set the flag SEE_MASK_NOCLOSEPROCESS so you will get a valid process handle you can wait for with WaitForSingleObject.
3. You must not let your program wait until execution is finnished, you can set a timer by SetTimer and examine the ready state of the program youve started.
4. Its always a good idea to check the error state of the execution, perhaps to show the user whats going wrong (file not found, not an executable and so on). Simplest way is to display a MessageBox. Therefore you can use the FormatMessage function to obtain a human readable error text.
Regards and good luck.
Share this answer
Member 7766180 27-Sep-11 23:14pm    
Thank You.
I don't want to wait for the batch to finish. So I'll just use ShellExecute() and let it run, because the main C++ needs to keep going and other instances of the batch will probably start before the first batch is finished.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900