Writing huge amounts of text to a textbox

后端 未结 3 1957
北荒
北荒 2020-12-16 18:58

I am writing a log of lots and lots of formatted text to a textbox in a .net windows form app.

It is slow once the data gets over a few megs. Since I am appending th

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-16 19:30

    StringBuilder will not help if the text box is added to incrementally, like log output for example.

    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.

    Notes:

    1. Create a class-scoped StringBuilder member (_sb)
    2. Start a timer (or use a counter)
    3. Append text updates to _sb
    4. When timer ticks or certain counter reached reset and append to text box
    5. restart process from #1

提交回复
热议问题