Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF : Assigning to RichTextBox.Document extremely slow (7 minutes!)

I'm constructing a formatted FlowDocument from XML. The XML is well formed and consists mainly of 10,000 nodes each with a single node with a 6 character string value.

Parsing the XML to an XElement and constructing the FlowDocument in memory takes about 5 seconds. Assigning the FlowDocument to the Document property of a RichTextBox in my application then takes about 7 minutes, and maxes out the CPU for that time.

Here is the relevant piece of code:

// The following six lines of code execute in about 5 seconds

var xml = XElement.Parse(response.Data);

PrettyXmlConverter px = new PrettyXmlConverter();
FlowDocument fd = px.Render(xml);

Paragraph p = new Paragraph();
p.Inlines.Add(new Run(response.TimeStamp.ToShortDateString() + " " + response.TimeStamp.ToLongTimeString()));
fd.Blocks.InsertBefore(fd.Blocks.ElementAt(0), p);

// This line of code takes about 7 minutes and maxes out the CPU for that time.
tbResponse.Document = fd;

I'm wondering what's going on here. I've profiled the code and see scores of thousands of calls to unmanaged methods such as fsFormatSubtrackBottomless and SubtrackFormatParaBottomless.

Can anyone shed any light on the problem, or come up with a workaround?

like image 712
Winston Smith Avatar asked Oct 14 '22 12:10

Winston Smith


1 Answers

Following excerpt from MacDonald's Pro WPF in C# 2010 book, p. 966:

The WPF RichTextBox, like most of the rich text controls that have preceded it, can be a bit sluggish. If you need to hold huge amounts of data, use intricate logic to handle key presses, or add effects such as automatic formatting (for example, Visual Studio’s syntax highlighting or Word’s spelling-checker underlining), the WPF RichTextBox probably won’t provide the performance you need.

like image 141
Sabuncu Avatar answered Oct 19 '22 01:10

Sabuncu