I'm trying to provide a live preview of XPS documents without hanging my UI thread. Opening the document is fast enough, but when I call GetFixedDocumentSequence(), my UI becomes unresponsive for several seconds while the document chugs away.
我试图提供XPS文档的实时预览,而不会挂起我的UI线程。打开文档的速度非常快,但是当我调用GetFixedDocumentSequence()时,我的UI会在文档突然消失的几秒钟内无响应。
// creating the doc is fine (0.005 seconds)
XpsDocument doc=new XpsDocument("BigFile.xps",FileAccess.Read);
// this hangs the UI for several seconds
FixedDocumentSequence seq=XpsDocument.GetFixedDocumentSequence();
// Once I have the sequence, GetPageAsync lets me pull out pages without breaking the UI
// ....
The obvious solution is to open the document on a worker thread, but the FixedDocumentSequence is tied to the thread that created it, so I can't access it from the UI thread, and if I try to call GetPageAsync from the worker thread I get an exception because DocumentPages contain visuals.
显而易见的解决方案是在工作线程上打开文档,但是FixedDocumentSequence与创建它的线程相关联,因此我无法从UI线程访问它,如果我尝试从工作线程调用GetPageAsync,我得到一个例外,因为DocumentPages包含视觉效果。
The only thing I can think of is to create the document on a seperate UI thread, break the document into pages, and then save those pages as XPS files that the UI thread opens. But that seems like a horribly complex solution. Does anyone know if there is an alternative way to getting the DocumentPages that does not rely on the FixedDocumentSequence?
我唯一能想到的是在单独的UI线程上创建文档,将文档分成页面,然后将这些页面保存为UI线程打开的XPS文件。但这似乎是一个非常复杂的解决方案。有没有人知道是否有另一种方法来获取不依赖于FixedDocumentSequence的DocumentPages?
1 个解决方案
#1
1
There is a simple solution. It's called multi-threaded UI and it helps you to do everything as you would do, but have two UI threads instead of one. It means that your XPS can load on a seperate UI thread without any problem. I've implemented it myself in the past, and it's good.
有一个简单的解决方案。它被称为多线程UI,它可以帮助您按照自己的方式执行所有操作,但有两个UI线程而不是一个。这意味着您的XPS可以在没有任何问题的情况下加载单独的UI线程。我过去自己实现了它,这很好。
http://blogs.msdn.com/b/dwayneneed/archive/2007/04/26/multithreaded-ui-hostvisual.aspx
#1
1
There is a simple solution. It's called multi-threaded UI and it helps you to do everything as you would do, but have two UI threads instead of one. It means that your XPS can load on a seperate UI thread without any problem. I've implemented it myself in the past, and it's good.
有一个简单的解决方案。它被称为多线程UI,它可以帮助您按照自己的方式执行所有操作,但有两个UI线程而不是一个。这意味着您的XPS可以在没有任何问题的情况下加载单独的UI线程。我过去自己实现了它,这很好。
http://blogs.msdn.com/b/dwayneneed/archive/2007/04/26/multithreaded-ui-hostvisual.aspx