C# 是否在进程运行时将进程的标准输出打印到文本框?
我有一个C GUI,从中运行一个python脚本大约需要2分钟。与其将Python脚本的输出直接指向一个文件,不如让GUI在进程运行时在文本框中显示Python脚本生成的所有打印输出。我找到的任何解决方案通常都会等待流程结束,然后再将标准输出重定向到文本框,我不确定是否正确搜索了解决方案。有人知道怎么做吗?以下是一些代码供参考:C# 是否在进程运行时将进程的标准输出打印到文本框?,c#,python,user-interface,output,C#,Python,User Interface,Output,我有一个C GUI,从中运行一个python脚本大约需要2分钟。与其将Python脚本的输出直接指向一个文件,不如让GUI在进程运行时在文本框中显示Python脚本生成的所有打印输出。我找到的任何解决方案通常都会等待流程结束,然后再将标准输出重定向到文本框,我不确定是否正确搜索了解决方案。有人知道怎么做吗?以下是一些代码供参考: using (Process proc = new Process()) { debug_output.AppendText("All debug outpu
using (Process proc = new Process())
{
debug_output.AppendText("All debug output will be listed below\n");
string pyFileName = "hello.py";
string args = "arg1";
proc.StartInfo.FileName = "C:\\Python27\\python.exe";
proc.StartInfo.Arguments = string.Format("{0} {1}", pyFileName, args);
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.OutputDataReceived += new DataReceivedEventHandler(MyProcOutputHandler);
proc.Start();
proc.BeginOutputReadLine();
while (!proc.HasExited)
{
Application.DoEvents();
}
}
使用以下处理程序:
private void MyProcOutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
{
if (!String.IsNullOrEmpty(outLine.Data))
{
if (debug_output.InvokeRequired)
{
debug_output.BeginInvoke(new DataReceivedEventHandler(MyProcOutputHandler), new[] { sendingProcess, outLine });
}
else
{
debug_output.AppendText(outLine.Data);
debug_output.AppendText("\n");
}
}
Console.WriteLine(outLine.Data);
}
作为一个更新,我尝试了从的解决方案,因为它看起来像完全相同的问题,但我仍然没有得到它的工作。我的输出结果是正确的,但只有在整个脚本运行完毕之后。请提供帮助。可能重复我已尝试了此帖子中的建议,但这些建议都无效,处理程序在流程退出后才被调用。