我正在我的应用程序中创建WPF报告,并且我发现,每次创建报告时,报告都会保留在内存中。我完全删除了文档内容,我只打印了一个空的UserControl。我将代码减少到最小,但它仍然在泄漏。你知道我做错了什么 - 或者我不做什么?
分析显示问题是ContextLayoutManager在ContextLayoutManager.LayoutQueue.Request中保留对FixedPage的引用
private AutoResetEvent _documentGenerated = new AutoResetEvent(false);
private Stream _outputStream;
private void button1_Click(object sender, RoutedEventArgs e)
{
using (_outputStream = new FileStream(@"c:\report_tst.xps", FileMode.Create))
{
Thread thread = new Thread(new ThreadStart(doWork));
thread.Name = "Report generating thread";
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
_documentGenerated.WaitOne();
}
}
private void doWork()
{
FixedDocument fixedDoc = new FixedDocument();
PageContent pageContent = new PageContent();
FixedPage fixedPage = new FixedPage();
fixedPage.Children.Add(new UserControl());
((IAddChild)pageContent).AddChild(fixedPage);
fixedDoc.Pages.Add(pageContent);
var package = Package.Open(_outputStream, FileMode.Create);
var xpsDoc = new XpsDocument(package, CompressionOption.Normal);
XpsDocumentWriter xpsWriter = XpsDocument.CreateXpsDocumentWriter(xpsDoc);
xpsWriter.Write(((IDocumentPaginatorSource)fixedDoc).DocumentPaginator);
xpsDoc.Close();
package.Close();
_documentGenerated.Set();
}
感谢您的任何建议。