6

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Mike Pateras
  • 14,715
  • 30
  • 97
  • 137

1 Answers1

12

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
  • 54,668
  • 9
  • 68
  • 101
  • Perfect! Any ideas on the output? – Mike Pateras Jun 22 '10 at 18:52
  • But how do I suppress the console window that pops up on for each process? – Mike Pateras Jun 22 '10 at 19:01
  • 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 Jun 22 '10 at 19:48
  • Yea, it's been a while since I used it, glad you got it working. – Aren Jun 22 '10 at 21:48
  • You need to set UseShellExecute to true for the Verb to be respected and it must be set to 'false' to redirect standard output. You can't do both. I'm pretty sure Windows also won't allow you to redirect standard input/output/error across the admin/non-admin security boundary. You'll have to find a different way to get output from the program running as admin - Reference: http://stackoverflow.com/a/8690661 – Kiquenet Aug 28 '14 at 06:39