Richtextbox.invoke,C#,表单仍然挂起
我已经编写了 ac# 应用程序来运行外部程序,并将其输出重定向到我的表单中的 Richtextbox。 我已经使用以下设置
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();
}
创建了该进程现在,虽然它正在工作,但是如果应用程序的输出很大,那么包含文本框的主窗体就会挂起。 我认为每次调用都会导致表单的重新绘制,这种情况经常发生。 有没有其他选择,以便我可以看到文本框的更新,并保持表单完全活动?
更新:
我想我得到了答案,当我应该使用Invoke
时,我使用了BeginInvoke
。
更新 1:
我尝试了 BeginInvoke 和 Suspendlayout,但它没有给我所需的功能,发生的情况是该进程已将所有标准输出返回到字符串,但负责更新文本的线程正在花费自己的时间打印数据。 我可以对它做点什么吗?
I've written a c# application to run an external program and i've redirectet it's output to a richtextbox in my form. I've created the process using the following settings
p1.StartInfo.RedirectStandardOutput = true;
p1.OutputDataReceived += new DataReceivedEventHandler(outputreceived);
and in the outputreceived event
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();
}
Now the thing is though it is working, but my main form which contains the textbox, hangs if the output is huge from the application. I think each time the invoke call leads to repainting of the form, which happens very frequently. Is there any alternative so that i can see the updates to the textbox as they happen and also keep the form completely active?
Update:
I think I got my answer, I used BeginInvoke
when I should have used Invoke
.
Update 1:
I tried both BeginInvoke and Suspendlayout but it is not giving me the desired functionality, what happens is that the process has returened all the standardoutput to the string, but the thread which is responsible for updating the text is taking it's own time to print the data. Can i do any thing to it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
既然您已经解决了您的问题,我只想指出,如果您使用
rtb.AppendText
(而不是Text += ...
),速度会更快并且使用 pinvoke 滚动到底部:Since you've already solved your problem, I'll just note that it will be faster if you use
rtb.AppendText
(instead ofText += ...
) and use pinvoke to scroll to the bottom:您可能想尝试
而不是
至少会使调用异步。 仍然不确定这是否会导致 UI 线程在绘制更新时锁定。
You might want to try
rather than
That will at least make the call Asynchronous. Still not sure if that will cause the UI thread to lock while the updates are being painted.
尝试暂停并恢复
richTextBox1
的布局我认为您应该使用 Debug.Print 来查看发生的情况。
Try to suspend and resume layout of
richTextBox1
I think you should use Debug.Print to view what's going on instead.
这是一篇旧帖子,但也许有人仍然像我一样在寻找它。
您也可以这样做,例如“for(writeToTextbox % 10 == 0)”,然后调用。
在这种情况下,它只会每 10 次更新一次。
更新:抱歉拼写错误! (wirte -> write)并感谢“HaveNoDisplayName”向我展示它!
It is an old post, but maybe somebody still looking for it like I did.
You also can do, like "for(writeToTextbox % 10 == 0)" then invoke.
In this case it will be updated only every 10 times.
UPDATE: sorry for the misspelling! (wirte -> write) and thanks for "HaveNoDisplayName" to show it to me!