Matthew
Matthew

Reputation: 2280

How to efficiently stream text to screen in WPF?

I want to stream a bunch of text to display the status/progress of a long running task (such as the output window in Visual Studio).

Currently I have something like this XAML:

    <ScrollViewer Canvas.Left="12" Canvas.Top="12" Height="129" Name="scrollViewer1" Width="678">
        <TextBlock Name="text"  TextWrapping="Wrap"></TextBlock>
    </ScrollViewer>

and this code behind:

    private void Update(string content)
    {
        text.Text += content + "\n";
        scrollViewer1.ScrollToBottom();
    }

After a while, it gets really slow.
Is there a recommended way of doing this type of thing? Am I using the right kinds of controls?

Thanks!

Upvotes: 3

Views: 3148

Answers (1)

Kent Boogaart
Kent Boogaart

Reputation: 178650

At a minimum, you'll want to use a readonly TextBox and use the AppendText() method to append text.

Of course, you're still not immune from performance problems if you have sufficient volumes of text. That being the case, you might need to look into virtualization (both data and UI) solution.

Upvotes: 5

Related Questions