Fastest way to append text to a TextBox
- From: "Phill W." <p-.-a-.-w-a-r-d-@xxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 02 Apr 2008 13:59:30 +0100
VB.Net 2005 SP1
Windows Forms Application
What's the fastest way to append text to a [Rich]TextBox?
I have an application that monitors data written to text [log] files. It needs to scan some fairly hefty files (typically ~15MB) and scanning the file pulling out the rows I want (typically ~10,000 of them) takes /comfortably/ under three seconds. In normal usage, this application will "follow" the end of these files as they are written to, so it needs to be able to scan [the end of] the file and update the screen in "real time".
However if, as I find them, I take each of these lines and append them to the end of a TextBox on a Form, the process slows to a crawl, red-lines my CPU and, even if I limit the TextBox to just 30KB of text, it takes over 90 seconds to complete.
(This will eventually be a RichTextBox because the output lines will need to be colour-coded as well, but I'm trying to start simple).
So, how can I get these lines into my TextBox /faster/?
Or, rather, why is updating the control /so/ slow, compared to hacking strings around in memory?
OK, OK, normally, even /I'd/ be the first to say
"who /needs/ to see 10,000 lines of data?"
but, I can think of at least three of my users who will want [the opportunity] to see every single one of them.
I have to say I was stunned at how fast VB.Net crunched its way through the data, but if getting that data up onto the screen is going to be /this/ slow, it's hardly worthwhile doing the "upgrade".
My "appending" code:
Private Sub AppendText(ByVal sText As String)
With Me.TextBox1
Dim iLen As Integer = .Text.Length + sText.Length
If (iLen >= (30000 * 1.1)) Then
Dim iTrimmer As Integer = iLen - 30000
Dim iPos As Integer = .Text.IndexOf(vbCrLf, iTrimmer)
.SelectionStart = 0
.SelectionLength = iPos + 1
.SelectedText = String.Empty
End If
.SelectionStart = .Text.Length
.SelectedText = sText & vbCrLf
.SelectionStart = .Text.Length
End With
End Sub
(Without the trimming bit, it's even more, /unbelievably/ slow; I got fed up waiting after five minutes.)
Any suggestions?
TIA,
Phill W.
.
- Follow-Ups:
- Re: Fastest way to append text to a TextBox
- From: aaron.kempf@xxxxxxxxx
- Re: Fastest way to append text to a TextBox
- From: Bill McCarthy
- Re: Fastest way to append text to a TextBox
- From: Cor Ligthert[MVP]
- Re: Fastest way to append text to a TextBox
- From: Steve Gerrard
- Re: Fastest way to append text to a TextBox
- From: jeff
- RE: Fastest way to append text to a TextBox
- From: AMercer
- Re: Fastest way to append text to a TextBox
- Prev by Date: Wrap Column Header Text In Listview
- Next by Date: Vista Installation
- Previous by thread: Wrap Column Header Text In Listview
- Next by thread: RE: Fastest way to append text to a TextBox
- Index(es):
Relevant Pages
|