Fastest way to append text to a TextBox



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.
.



Relevant Pages

  • Re: Fastest way to append text to a TextBox
    ... What's the fastest way to append text to a TextBox? ... Dim iLen As Integer = .Text.Length + sText.Length ... Dim iTrimmer As Integer = iLen - 30000 ... .SelectedText = String.Empty ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Parsing / " characters
    ... have the rest of the code to append the ... - JCL inserts that string at the start of every program, ... >>the code associated with that program - in exact order, ... >>Dim db As Database, tdf As TableDef ...
    (microsoft.public.access.externaldata)
  • Re: Fastest way to append text to a TextBox
    ... What's the fastest way to append text to a TextBox? ... CPU and, even if I limit the TextBox to just 30KB of text, it takes over ... Private Sub AppendText(ByVal sText As String) ... Dim iLen As Integer = .Text.Length + sText.Length ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Inserting a line at the beginning of a Text file
    ... Dim intTarget As Integer ... Dim strSource As String ... > The next thing that happens is that I open that text file up to add a> variable to it, however I can only either overwrite the file using the> Output method therefore losing the original table that was exported> and the file only contains the variable, or I can Append to the text> file, however append adds the variable as the last line in the file> and I need the variable to be the first line in the file. ...
    (microsoft.public.access.externaldata)
  • DOM CreateElement?
    ... Sub Page_Load ... Dim xmlDoc As New XmlDocument ... Dim eltProducts1 As XmlElement = xmlDoc.CreateElement ... AppendData.xml, I have used 2 different variables to append the 2 sets ...
    (microsoft.public.dotnet.framework.aspnet)