I need to run several instances of an external executable from my app. The average run time for this executable is about 3 minutes.
I want to redirect output from these processes, and update a progress bar in my GUI.
Of course I don’t want to wait for them to return before I can continue using my app.
I think I should create a thread for every instance, and update my progress bar when a thread finishes.
Is this the right approach ?
Also, do you recommend a good resource / documentation to understand how it works ? I’ve found http://www.dotnetperls.com/threadpool only.
edit : these processes are network-based, ie: the run time may vary a lot depending on the link latency/bandwidth.
Concerning the progress bar, I would like to update it every time a process finishes. Is there a handler for that ? Later i will add more detailed update, based on the processes output to increase the progress done at each execution step.
edit 2 :
Thanks for your inputs. As I may have to run a lot of process (up to 20), and I don’t want to saturate bandwidth, i’ll run 5 in parallel max. Every time a process finishes, I increment the progress counter (for my progress bar) and I run another one until they’re all completed, using :
Process p = new Process();
p.StartInfo.FileName = pathToApp;
p.EnableRaisingEvents = true;
p.Exited += OnCalibrationProcessExited;
p.Start();
private void OnCalibrationProcessExited(object sender, EventArgs e)
{
runAnotherOne function
}
Is it correct or is there a more elegant way to achieve this ?
I don’t want my app to be blocked during execution of course.
Is it better to use background workers for this ?
You should be using
ProcessandProcessStartInfo.You’ll need to set
ProcessStartInfo.UseShellExecutetofalse,ErrorDialogtofalse,RedirectStandardOutputtotrue(and possiblyRedirectStandardErrortoo).You’ll also need to provide a delegate to the Process object to handle to output generated by the external process through
OutputDataReceived(and possiblyErrorDataReceivedas well).There’s also an
Exiteddelegate you can set that will be called whenever the process exits.Example: