快速将文本追加到文本框 [英] Fast Append Text to text box
问题描述
我有一个BackgroundWorker
线程正在GUI中的文本框上使用BeginInvoke
发布消息.在文本框中显示文本的方法write_debug_text
使用AppendText
并将文本写入Console
.
I have a BackgroundWorker
thread that is posting messages, using BeginInvoke
on a textbox in the GUI. The method, write_debug_text
, that displays text in the textbox uses AppendText
and also writes the text to the Console
.
外观是BackgroundWorker
的写入速度太快,导致write_debug_text
无法跟上.我在write_debug_text
处设置了一个断点,必须等待很长时间才能被击中.在到达断点之前,会多次调用"BeginInvoke".
The appearance is that the BackgroundWorker
is writing too fast for the write_debug_text
to keep up. I set a breakpoint at write_debug_text
and have to wait a long time before it is hit. Many calls to 'BeginInvoke` occur before the breakpoint is hit.
我正在寻找UI上实时显示消息的方式,就像VS C#Express IDE中的System.Console
一样.
I'm looking for a real-time display of messages on the UI, much like the System.Console
in the VS C# Express IDE.
通过搜索SO,我了解到AppendText
是使用速度更快的方法,并且可能必须重新分配字符串.
From searching on SO, I understand that AppendText
is the faster method to use and that strings may have to be reallocated.
一些答复建议使用StringBuilder
,然后定期将该文本写入文本框.但这需要添加更多的事件和计时器.我宁愿不这样做(我的简单应用程序越来越复杂).
Some replies suggest using StringBuilder
then periodically writing that text to the textbox. But this requires adding more events and timers; which I would rather not do (my simple application keeps getting more and more complex).
如何将实时信息实时写入文本框(并显示在文本框中)?
How can I either write real-time to the Textbox (and have it display)?
我目前的想法是创建一个从Textbox继承的小部件,该小部件使用文本队列和计时器.
My current idea is to create a widget inheriting from Textbox that uses a text queue and timer.
这是我的代码的一部分:
Here is a fragment of my code:
private m_textbox;
//...
m_textbox.BeginInvoke(new write_debug_text_callback(this.write_debug_text),
new object[] { debug_text });
return;
private void write_debug_text(string text)
{
string time_stamp_text = "";
time_stamp_text = DateTime.Now.ToString() + " ";
time_stamp_text += text;
m_textbox.AppendText(time_stamp_text);
m_textbox.Update();
System.Console.Write(time_stamp_text);
return;
}
我尝试将BeginInvoke
更改为Invoke
,并且我的应用程序已挂起.当我使用调试器暂停/中断时,执行指针位于对Invoke
的调用中.
I have tried changing BeginInvoke
to Invoke
and my application is hung. When I pause / break using the debugger, the execution pointer is on the call to Invoke
.
顺便说一句,我在Java,C ++和C方面有多年的经验.我在C#的第5个月了.
BTW, I've got many years experience with Java, C++, and C. I am on my 5th month with C#.
推荐答案
我使用了@Jeffre L. Whitledge的建议并减少了字符串分配的数量.由于这是一个封闭的应用程序,具有固定数量的字符串,因此我缓存了这些字符串.这产生了我的程序执行快得多的副作用.
I used @Jeffre L. Whitledge's advice and reduce the number of string allocations. Since this is a closed application with a fixed number of strings, I cached the strings. This produced a side-effect of my program executing significantly faster.
我的问题之一仍然是Windows响应消息的速度较慢.当进度条更新时可以看到.发送消息(例如添加文本)和执行消息之间存在一定的延迟.
One of my issues is still the slowness in Windows responding to messages. This can be seen when the progress bar is updated. There is a definite delay from when a message is sent (such as append text), and when it is performed.
这篇关于快速将文本追加到文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!