StringBuilder may not be here, and is usually used when you add a lot of data to a loop or another process and want to call ToString at the very end. Performance in this case is better than, say, performing a bunch of explicit string concatenations due to memory overhead.
I'm not quite sure about your application, but I think you just want to display some output from the terminal. I suggest you save the lines of text in the ObservableCollection property and add to it when you get new lines. I am doing something similar and have an extended RichTextBox with a DataSource property that I use to display rows. I'm sure there are faster ways to do this (RichTextBox is NOT known for its performance, and I build a new inline block every time a line is added), but I never noticed any performance issues - and this allowed me to get creative with color encoding formatted based on event type, etc.
In any case, I did my best to copy what I have, while I remove the details of my specific implementation, I did not test it specifically.
EventLogBox Class:
public class EventLogBox : RichTextBox { public EventLogBox() { } static EventLogBox() { RichTextBox.IsReadOnlyProperty.OverrideMetadata( typeof(EventLogBox), new FrameworkPropertyMetadata(true)); RichTextBox.VerticalScrollBarVisibilityProperty.OverrideMetadata( typeof(EventLogBox), new FrameworkPropertyMetadata(ScrollBarVisibility.Visible)); RichTextBox.FontFamilyProperty.OverrideMetadata( typeof(EventLogBox), new FrameworkPropertyMetadata(new FontFamily("Courier New"))); } private void AddItem(string pEntry) { Paragraph a = new Paragraph(); Run messageRun = new Run(pEntry); a.Inlines.Add(messageRun); this.Document.Blocks.Add(a); this.ScrollToEnd(); } private void LoadNewItems() { this.Document.Blocks.Clear(); foreach (string entry in DataSource) { this.AddItem(entry); } }
Property in ViewModel
private ObservableCollection<string> mLines = new ObservableCollection<string>(); public ObservableCollection<string> Lines { get { return mLines; } }
In XAML:
<local:EventLogBox DataSource="{Binding Path=Lines}" Height="100"/>
Then, when you get a new line from your terminal, call
Lines.Add(newLine);
source share