2

I have a TextBox which is bound to a string in my ViewModel.

XAML

<TextBox x:Name="TracesTextBox" IsReadOnly="True" Text="{Binding Traces, Mode=OneWay}" UndoLimit="0" IsUndoEnabled="False" Margin="5" 
VerticalScrollBarVisibility="Auto" FontSize="10" TextChanged="OnTextBoxTextChanged" HorizontalScrollBarVisibility="Auto"
BorderBrush="{DynamicResource AccentColorBrush}"/>

Also I do something in the code behind to scroll the Text Box down as and when the string gets updated so that I can see the last string appended

Code Behind

private void OnTextBoxTextChanged(object sender, TextChangedEventArgs e)
{
    TracesTextBox.CaretIndex = TracesTextBox.Text.Length;
    TracesTextBox.ScrollToEnd();
}

This piece of code gets executed by a Background thread which fetches data from Ethernet

ViewModel Code Invoked by Background thread

var str =((float)(msg.ArrivalTimeMs / 1000.0)).ToString("00000.000").PadRight(16, ' ') ;
str+= msg.Bytes.Take(msg.len).Aggregate(str, (current, dataByte) => current + (dataByte.ToString("X").PadLeft(2, '0') + " ")) + "\n";
UiTaskFactory.StartNew(() =>
{
    Traces += str;
}
);

Problem is that after several hours of running the application the amount of text in the Traces gets very big and the UI does not respond to the scroll at all.

Now, how do I perform some kind of stripping at the beginning of the string and keep the last thousand lines that I received? I suppose this is gonna cost in terms of Processor usage. If I received fewer messages from Ethernet the scrolling is very responsive. I thought of using Queues of strings but isn't there a more elegant solution of working with several thousands of lines of string?

WinterS
  • 127
  • 11
  • 4
    A `TextBox` is not appropriate for what you're doing here. See [my example](http://stackoverflow.com/a/16745054/643085) of how to do that properly in WPF using a virtualized ItemsControl. – Federico Berasategui Apr 02 '15 at 17:28
  • I will try this out and will let you know soon. – WinterS Apr 03 '15 at 04:32
  • @HighCore I am solving another performance bottleneck before I move to this, do you think you can help me, as you can tell I am new to wpf and I already said yes to the Boss so I had to make it work. :(. The link is here http://stackoverflow.com/questions/29431988/observablecollection-triggering-too-many-collectionchanged – WinterS Apr 04 '15 at 14:07
  • I tried it, it is really responsive. Now I chucked out the text string thing. I have customized the fields and Bindings to my project. Did some formatting and then, I am triggering it something like 1500 times per second. It is good. :D. Can you reply I will mark it as answer. Thank you. – WinterS Apr 04 '15 at 21:55

0 Answers0