我正在从XML构建格式化的FlowDocument。该XML格式正确,主要由10,000个节点组成,每个节点都有一个包含6个字符字符串值的单一节点。
将XML解析为XElement并在内存中构建FlowDocument大约需要5秒钟的时间。然后将FlowDocument分配给应用程序中RichTextBox的Document属性需要大约7分钟,并且在此期间会使CPU达到最大值。
以下是相关代码片段:
将XML解析为XElement并在内存中构建FlowDocument大约需要5秒钟的时间。然后将FlowDocument分配给应用程序中RichTextBox的Document属性需要大约7分钟,并且在此期间会使CPU达到最大值。
以下是相关代码片段:
// 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;
我想知道这里发生了什么。我已经对代码进行了分析,发现有成千上万次调用未受控方法,例如fsFormatSubtrackBottomless
和SubtrackFormatParaBottomless
。
有人能够解决这个问题或提出解决方法吗?