从后台线程在UI线程中编程创建UI并将其复制到后台线程?
问题描述:
这里是我开始了一个新的线程:从后台线程在UI线程中编程创建UI并将其复制到后台线程?
private void Button_Click_1(object sender, RoutedEventArgs e)
{
System.Threading.Thread myThread = new System.Threading.Thread(prePrint);
myThread.Start();
}
预印本功能是这样的:
private void prePrint()
{
for (int j = 0; j < DataHandle.Recipe.Count; j++)
{
// create print dialog
// create print ticket.
FlowDocument fd = new FlowDocument();
// assign the createFD(int j) to fd here. << HERE IS THE MAIN PROBLEM
DocumentPaginator sd = ((IDocumentPaginatorSource)fd).DocumentPaginator;
// print the flow document here
}
}
private FlowDocument createFD(int j) {
FlowDocument fd = new FlowDocument();
return fd;
}
我想创建UI线程流文档,并将其复制到后台线程,并最终打印它(如果可能)。
我对这项技术很陌生。请帮助我找到更好的方法。
答
那么,基本的技巧是使用Dispatcher
的FlowDocument
和调用函数BeginInvoke()
。顺便说一下,只能在UI线程上创建文档,并在创建它的线程上使用该文档,但如果文档结构首次构建并保存到内存XML
后,可以对其进行优化。怀疑这一点的表现,你应该检查它。
IMO瞧,一个很好的例子:http://chrismylonas.blogspot.com/2007/12/flowdocument-and-multiple-threads.html
不会保存到内存会影响性能? – user995387 2011-12-30 08:24:45
我想是的。但是尽可能多的保存内存,以便在线程之间编组保存的流。但这是否与你有关,取决于你的应用程序。你应该测量它。如果文档太大,您可以尝试将其保存到临时的“MemoryMappedFile”中,然后从中加载。 *可能会更快*。但措施。 – Tigran 2011-12-30 08:31:36