开发者

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

开发者 https://www.devze.com 2023-01-04 14:39 出处:网络
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

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 defa开发者_JS百科ult output?


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);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号