I am trying to add a output monitor to my WPF application. A read only monitor similar to the debug output in visual studio.
Is there a WPF control which already provides the functionality I need? Or is there a way I could reuse the control from Visual Studio?
Currently I'm using a standard TextBox backed by a StringBuilder. Updates go to the StringBuilder while the TextBox gets the newest string every 200ms.
My problem is that this gets really slow as the output string gets longer.
To see the debug output window, in Microsoft Visual Studio, click View, click Other Windows, and then click Output. You can view the debug output in this window only if the debugger is attached to the process that is writing to the output window.
The Output window is where many of the tools, including the compiler, send their output. Every time you start an application, a series of messages is displayed in the Output window. These messages are generated by the compiler, and you need not understand them at this point.
I would use the RichTextBox control to output the data.
In this sample I had no problem with performance at all.
public partial class MainWindow : Window
{
private int counter = 0;
public MainWindow()
{
InitializeComponent();
Loaded+=OnLoaded;
}
private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
{
for (int i = 0; i < 200; i++)
{
AddLine(counter++ + ": Initial data");
}
var timer = new DispatcherTimer();
timer.Interval = new TimeSpan(0, 0, 0, 0, 200);
timer.Tick += TimerOnTick;
timer.IsEnabled = true;
}
private void TimerOnTick(object sender, EventArgs eventArgs)
{
AddLine(counter++ + ": Random text");
}
public void AddLine(string text)
{
outputBox.AppendText(text);
outputBox.AppendText("\u2028"); // Linebreak, not paragraph break
outputBox.ScrollToEnd();
}
}
And XAML
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<RichTextBox x:Name="outputBox"
VerticalScrollBarVisibility="Visible"
HorizontalScrollBarVisibility="Visible"
IsReadOnly="True">
<FlowDocument/>
</RichTextBox>
</Grid>
</Window>
And it's probably easy to extend it. If the scroll position is not at the end, do not Scroll to the end, for example, so that you can view old data while the text box is still updating.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With