RE: Fastest way to append text to a TextBox



Update the rtb in with batches of lines, not single lines. The performance
behavior you don't like will come with any control, eg updating a heavily
loaded list box one line at a time will crawl too.

"Phill W." wrote:

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

  • catch values of dynamic controls
    ... Dim page As Control ... If TypeOf ctl Is PageView Then ... If TypeOf ctl Is TextBox Then ...
    (microsoft.public.dotnet.framework.aspnet)
  • force the binding Parse property
    ... I have a form in which I have a custom textbox control that gives me a new ... Public Sub New ... Dim binding As Binding = Me.DataBindings ...
    (microsoft.public.dotnet.languages.vb.controls)
  • RE: Datagrid cancel command does not work when adding new record
    ... If I edit a record then press the cancel button this works fine. ... Dim UserID, UserName, UserNetwork, UserType As String ... ' control in each cell -- a TextBox control. ...
    (microsoft.public.dotnet.framework.aspnet.webcontrols)
  • Re: Newbie question - VB.net, referencing controls
    ... The DirectCast to Textbox is not needed in this situation. ... Text is a in textbox derived property from control and can therefore direct ... Dim ctr As Control ...
    (microsoft.public.dotnet.languages.vb)
  • Datagrid cancel command does not work when adding new record
    ... If I edit a record then press the cancel button this works fine. ... Dim UserID, UserName, UserNetwork, UserType As String ... ' control in each cell -- a TextBox control. ...
    (microsoft.public.dotnet.framework.aspnet.webcontrols)

Loading