Дождитесь ввода от вызываемого приложения в C#
У меня есть программа из третьей части под названием Sample.exe, которую я хотел бы запустить из моего приложения на C#. Код для запуска программы приведен ниже. Проблема в том, что код не ждет ввода программы и пропускает его. Мне нужно дождаться ввода, когда отображается первое меню, а затем дождаться ввода, когда появится второе меню. Как я мог это сделать?
Process sortProcess = new Process();
sortProcess.StartInfo.FileName = @"Sample.exe";
// Set UseShellExecute to false for redirection.
sortProcess.StartInfo.UseShellExecute = false;
// Redirect the standard output of the sort command.
// This stream is read asynchronously using an event handler.
sortProcess.StartInfo.RedirectStandardOutput = true;
StringBuilder sortOutput = new StringBuilder("");
// Set our event handler to asynchronously read the sort output.
sortProcess.OutputDataReceived += new DataReceivedEventHandler(SortOutputHandler);
// Redirect standard input as well. This stream
// is used synchronously.
sortProcess.StartInfo.RedirectStandardInput = true;
// Start the process.
sortProcess.Start();
// Use a stream writer to synchronously write the sort input.
StreamWriter sortStreamWriter = sortProcess.StandardInput;
// Start the asynchronous read of the sort output stream.
sortProcess.BeginOutputReadLine();
// End the input stream to the sort command.
sortStreamWriter.Close();
// Wait for the sort process to write the sorted text lines.
sortProcess.WaitForExit();
sortProcess.Close();
}
private static void SortOutputHandler(object sendingProcess,
DataReceivedEventArgs outLine)
{
// Collect the sort command output.
if (!String.IsNullOrEmpty(outLine.Data))
{
string str = outLine.Data;
Console.WriteLine(str);
}
}