我已经编写了 ac# 应用程序来运行外部程序,并且我已经将它的输出重定向到我的表单中的富文本框。我使用以下设置创建了流程
p1.StartInfo.RedirectStandardOutput = true;
p1.OutputDataReceived += new DataReceivedEventHandler(outputreceived);
并在 outputreceived 事件中
void outputreceived(object sender, DataReceivedEventArgs e)
{
if (!string.IsNullOrEmpty(e.Data))
{
richTextBox1.Invoke(new UpdateOutputCallback(this.updateoutput),
new object[] { e.Data });
}
}
void updateoutput(string text)
{
int len = text.Length;
int start = richTextBox1.Text.Length;
richTextBox1.Text += text + Environment.NewLine;
richTextBox1.Select(start, len);
richTextBox1.SelectionColor = System.Drawing.Color.White;
richTextBox1.Select(richTextBox1.Text.Length, 0);
richTextBox1.ScrollToCaret();
}
现在问题是虽然它正在工作,但是如果应用程序的输出很大,我包含文本框的主表单会挂起。我认为每次调用调用都会导致重新绘制表单,这种情况经常发生。是否有任何替代方法可以让我看到文本框的更新,并保持表单完全激活?
更新:
我想我得到了答案,我BeginInvoke
在应该使用的时候使用了Invoke
.
更新1:
我尝试了 BeginInvoke 和 Suspendlayout 但它没有给我所需的功能,发生的情况是该过程已将所有标准输出恢复到字符串,但负责更新文本的线程正在花费自己的时间来打印数据. 我可以对它做点什么吗?