【问题标题】:Programatically create UI in UI thread from Background thread and copy it to background thread again?从后台线程以编程方式在 UI 线程中创建 UI 并将其再次复制到后台线程?
【发布时间】:2011-12-30 08:04:35
【问题描述】:

这是我开始一个新线程的方式:

  private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            System.Threading.Thread myThread = new System.Threading.Thread(prePrint);
            myThread.Start();                    
        }

prePrint 函数是这样的:

 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 线程中创建流文档并将其复制到后台线程并最终打印(如果可能)。

我对这项技术非常陌生。请帮助我找到更好的方法。

【问题讨论】:

    标签: c# .net wpf multithreading


    【解决方案1】:

    嗯,基本技巧是使用DispatcherFlowDocument 并使用BeginInvoke() 调用函数。顺便说一下,文档只能在 UI 线程上创建并在创建它的线程上使用,但是如果首先构造文档结构并在将其保存到内存中 XML 之后,则可以对此进行优化。对这个性能有点怀疑,你应该检查一下。

    看这里,一个很好的例子:http://chrismylonas.blogspot.com/2007/12/flowdocument-and-multiple-threads.html

    【讨论】:

    • 我认为是的。但是在内存中的节省与在线程之间编组保存的流一样多。但这是否与您相关,取决于您的应用程序。你应该测量它。如果文档太大,您可以尝试将其保存到临时的MemoryMappedFile 并在从中加载后。 可以更快。但要衡量。
    猜你喜欢
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-24
    • 1970-01-01
    • 1970-01-01
    • 2020-05-19
    • 1970-01-01
    相关资源
    最近更新 更多