C#等待重定向输出的所有输出数据,然后继续
我有一个应用程序,它等待用户的命令(比如cmd)。当用户按Enter确认其命令时,程序启动名为work()的方法。此方法启动输出重定向到richtextbox的进程。我正在使用Invoke将输出实时写入richtextbox。但我的问题是,即使在方法结束后,程序的输出也会显示出来。如何等待输出结束,然后用提示符写新行? 我欢迎任何关于我的英语的建议和抱歉 调用方法C#等待重定向输出的所有输出数据,然后继续,c#,winforms,invoke,C#,Winforms,Invoke,我有一个应用程序,它等待用户的命令(比如cmd)。当用户按Enter确认其命令时,程序启动名为work()的方法。此方法启动输出重定向到richtextbox的进程。我正在使用Invoke将输出实时写入richtextbox。但我的问题是,即使在方法结束后,程序的输出也会显示出来。如何等待输出结束,然后用提示符写新行? 我欢迎任何关于我的英语的建议和抱歉 调用方法 if (e.KeyChar == (char)13) //Enter { work(); richTextBox
if (e.KeyChar == (char)13) //Enter
{
work();
richTextBoxOutput.AppendText(prompt + " ");
}
方法工作()
StringBuilder output = new StringBuilder();
Process proc = new Process();
proc.StartInfo.FileName = command;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.Arguments = arguments;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.WorkingDirectory = workingDirectory;
proc.StartInfo.CreateNoWindow = true;
proc.OutputDataReceived += new DataReceivedEventHandler(
(s, e) =>
{
if (richTextBoxOutput.InvokeRequired)
{
richTextBoxOutput.Invoke(new Action(() => richTextBoxOutput.AppendText(e.Data + "\n")));
}
else
richTextBoxOutput.AppendText(e.Data + "\n");
}
);
proc.ErrorDataReceived += new DataReceivedEventHandler((s, e) => { richTextBoxOutput.AppendText(e.Data + "\n"); });
proc.Start();
proc.BeginOutputReadLine();
this.setTitle("Working ...");
while (!proc.HasExited)
{
Application.DoEvents(); // This keeps your form responsive by processing events
}