I need to do this in my c# program.
Process process = LaunchCommandWindow("Special args and environment set")
process.StandardInput.WriteLine("cmd1");
process.StandardInput.WriteLine("cmd2");
//Parse logs
using (Streamreader sr = new ())
{ etc etc..}
My commands execute correctly, but the problem is that Parse logs code doesnt wait for cmd1 and cmd2 execution to complete. I cannot call exit and process. waitforexit because, after parsing the logs, I have more commands to execute.
Here is how process start info is set. LaunchCommandWindow()
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.Fi开发者_如何学CleName = Path.Combine(Environment.SystemDirectory, "cmd.exe");
startInfo.Arguments = "/K " + newEnvironmentArgs;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardInput = true;
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
return process;
How do I get the parse logs code to wait for cmd2 completion?
Thanks.
If "cmd2" writes a completion to the Standard Output stream, you could listen on Standard Output until you see that event, then process from there.
Create a new thread to run the tasks.
Use a BackgroundWorker
.
When it finishes, it raises an event (in your main thread) from which you can continue running.
This will prevent your program from hanging, and if there is other work to be done, it can allow you to do it while the other thread runs.
Here's a tutorial
-----Edit-----
After further research, here's a thought for the rest:
You do have to set RedirectStandardOutput
to true, if you want to read the output.
If you don't have to run both commands in the same window, you can call
process.WaitForExit(int)
where the int is the number of milliseconds to wait, then launch a new process (or process
with new arguments) to fire cmd2.
I think Process.WaitForExit() method is just what you need. There is also Process.HasExited property.
And for launching use something like that "cmd /C cmd1 & cmd2"
精彩评论