【发布时间】:2021-12-05 20:42:50
【问题描述】:
我正在使用 microsoft 的示例来使用异步方法处理 stdout 和 stderr: https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.datareceivedeventhandler?view=net-5.0
但是,如何在主窗体中更新文本框的文本属性?因为,它不允许线程在另一个线程中更新控制。即,我希望下面的这个处理程序更新 frmMain.txtBox1.text 在文本框中显示标准输出日志。
private static void SortOutputHandler(object sendingProcess,
DataReceivedEventArgs outLine)
{
// Collect the sort command output.
if (!String.IsNullOrEmpty(outLine.Data))
{
numOutputLines++;
// Add the text to the collected output.
sortOutput.Append(Environment.NewLine +
$"[{numOutputLines}] - {outLine.Data}");
}
}
【问题讨论】:
-
或者只是简单的Process.SynchronizingObject = Application.OpenForms[0];