Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run processes synchronously, targeting the same output?

I have a .Net application that needs to run several executables. I'm using the Process class, but Process.Start doesn't block. I need the first process to finish before the second runs. How can I do this?

Also, I'd like all of the processes to all output to the same console window. As it is, they seem to open their own windows. I'm sure I can use the StandardOutput stream to write to the console, but how can I suppress the default output?

like image 448
Mike Pateras Avatar asked Jun 22 '10 18:06

Mike Pateras


1 Answers

I believe you're looking for:

Process p = Process.Start("myapp.exe");
p.WaitForExit();

For output:

StreamReader stdOut = p.StandardOutput;

Then you use it like any stream reader.

To suppress the window it's a bit harder:

ProcessStartInfo pi = new ProcessStartInfo("myapp.exe");
pi.CreateNoWindow = true;
pi.UseShellExecute = true;

// Also, for the std in/out you have to start it this way too to override:
pi.RedirectStandardOutput = true; // Will enable .StandardOutput

Process p = Process.Start(pi);
like image 167
Aren Avatar answered Sep 20 '22 01:09

Aren