Simple Text Display Window

I am trying to find/develop a very lightweight method to display text in a pane for my desktop application. Other than setting a single font for all text, I have no requirements for text formatting. I have tried using a TextBox, TextBlock, and various observable collections. Computational efficiency is paramount.

The text is created by the application, and is never modified by the user. The overriding requirement is that the application typically generates text at from 100 to 700 lines per second with a peak rate of 1900 lines per second. It is not unusual for the application to run for several days. Ideally, the display would scroll continuously so that the last line added was always visible.

My current implementation uses a TextBox (For longer periods, I currently have no choice but to disable text display). Things I have tried include:

  • setting IsUndoEnabled="False"
  • removing the first line of text for every line of text added after n (usually 1000). This was really ugly and prompted me to try redesigning it as MVVM.
  • using an Observable Collection or a BindingList with a ListView (ala MVVM) (Suprisingly this was worse than the TextBox even with the following)
  • suppressing collection/list changed so that UI updates were deferred to 'idle' periods.

I can provide more details on what I have tried, but I didn't want to start by writing a book.

This is my current XAML implementation:

<TextBox Grid.Row="1" Name="tbTraceLog" IsReadOnly="True" IsUndoEnabled="False" VerticalScrollBarVisibility="Auto" FontFamily="Courier New"/>

And in codebehind:

    for (icount = 0; icount < tlData.stringcount; icount++)
        tbTraceLog.AppendText(tlData.s[icount]);          // TextBox implementation
        if (tbTraceLog.LineCount > 1000)
            // Remove one line
            tbTraceLog.Text = tbTraceLog.Text.Substring(tbTraceLog.Text.IndexOf('\n')+1);

Is there a more basic text rendering class where I can start? What classes does TextBlock/TextBox inherit from?


I don't believe the control selection is the issue, I believe your issue is the performance related to maintaining string concatenation. I'm assuming that since you attempted to remove the first entry to add a new one after 1000 that you do not need to keep a history. Nor handling the max 1900 messages generated per 1 second is a requirement.

I would keep the TextBox as your control. Make it multi-line and readonly.

Create a class level List for your results:

List<string> list = new List<string>();

Create a public string Property:

public string Result 
        return string.Concat(list); 

In your XAML, bind the TextBox.Text to the Result property:

<TextBox Text="{Binding Path=Result}" />

Next create a class level boolean called update:

private bool update = false;

When you receive the result to concatenate, check if list.Count > 1000. If it is, list.RemoveAt(0) and then list.Add(newTextToAdd + Environment.NewLine). Then set update to true. Please note I'm assuming you receive the text to add one at a time. If you have a collection of text received, you can calculate the how many to remove, then call list.AddRange(collection).

if (list.Count > 1000)
    list.Add(newTextToAdd + Environment.NewLine);
update = true;

Create a timer. I would say set it to fire 1 second. Then, when it fires check if we updated our List. If so raise the Result property changed notification and then set update to false:

if (update)
    update = false;

That should do it. I think the string.Concat should be fast enough to handle your load if you have it on the 1 second timer. I think using the timer is acceptable because the application generates X amount of messages per second. Queueing into the List should also be okay because you're only going to miss seeing text if it generates more than 1000 in a second. Since you already attempted to limit the max to 1000, supporting more doesn't appear to be a requirement.

Need Your Help

Is it possible to install App store build from xcode to test phone?

iphone xcode debugging app-store

I am not sure of it. It seems for one of my app I can install the app store target to my phone. But cannot debug.

wpf databinding and x:Arrays

wpf arrays data-binding

I try to make some databinding involving an x:Array defined in the resources, but somehow, it doesn't work even the code is compiling.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.