问题描述
我正在将大量格式化文本的日志写入 .net windows 窗体应用程序中的文本框.
I am writing a log of lots and lots of formatted text to a textbox in a .net windows form app.
一旦数据超过几兆,它就会变慢.由于我要附加字符串,因此每次都必须重新分配对吗?我只需要将值设置为文本框一次,但在我的代码中我正在执行 line+=data
数万次.
It is slow once the data gets over a few megs. Since I am appending the string has to be reallocated every time right? I only need to set the value to the text box once, but in my code I am doing line+=data
tens of thousands of times.
有没有更快的方法来做到这一点?也许不同的控制?有没有可以使用的链表字符串类型?
Is there a faster way to do this? Maybe a different control? Is there a linked list string type I can use?
推荐答案
如果文本框以增量方式添加,例如日志输出,StringBuilder 将无济于事.
StringBuilder will not help if the text box is added to incrementally, like log output for example.
但是,如果上述情况属实,并且如果您的更新足够频繁,则您可能应该缓存一些更新,然后一步添加它们(而不是不断地添加).这将为您节省许多字符串重新分配......然后 StringBuilder 会很有帮助.
But, if the above is true and if your updates are frequent enough it may behoove you to cache some number of updates and then append them in one step (rather than appending constantly). That would save you many string reallocations... and then StringBuilder would be helpful.
注意事项:
- 创建一个类范围的 StringBuilder 成员 (_sb)
- 启动计时器(或使用计数器)
- 将文本更新附加到 _sb
- 当计时器滴答声或某个计数器达到重置并附加到文本框
- 从 #1 重启进程
这篇关于将大量文本写入文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!