views:

31

answers:

1

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?

+5  A: 

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);
Aren
Perfect! Any ideas on the output?
Mike Pateras
Updated for output.
Aren
But how do I suppress the console window that pops up on for each process?
Mike Pateras
Updated again. :)
Aren
I've got it working now. Thank you for your help. It should be noted that I got an exception when I had both RedirectStandardOutput and UseShellExecute set to true. Also, once I redirected the output, I could no longer use WaitForExit. It would block indefinitely. Reading from StandardOutput, however, solves this problem.
Mike Pateras
Yea, it's been a while since I used it, glad you got it working.
Aren