在其他UI线程中打印DocumentViewer的内容 [英] Printing the content of a DocumentViewer in a different UI thread

查看:247
本文介绍了在其他UI线程中打印DocumentViewer的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的WPF应用程序中,我有一个特定的Window,其中除其他控件外还包含一个DocumentViewer.

打开并加载此窗口后,它将动态生成带有进度指示器的FixedDocument,然后将其显示在DocumentViewer中.它可以正常工作,并且为了改善用户体验,我在自己的线程中运行此窗口,以便在构建文档时主应用程序窗口仍然可以响应.

基于该网页,我在这样的新线程中打开窗口:

public void ShowDocumentViewerWindow(params object[] data) {
    var thread = new Thread(() => {
        var window = new MyDocumentViewerWindow(new MyObject(data));
        window.Closed += (s, a) => window.Dispatcher.InvokeShutdown();
        window.Show();
        System.Windows.Threading.Dispatcher.Run();
    });
    thread.SetApartmentState(ApartmentState.STA);
    thread.Start();
}

到目前为止,我对此设置感到满意,但是我遇到了一个问题.

MyDocumentViewerWindow包含一个打印按钮,该按钮引用针对DocumentViewer的内置打印"命令:

<Button Command="Print" CommandTarget="{Binding ElementName=MyDocumentViewer}">Print</Button>

在将窗口放置在自己的线程中之前,此方法工作正常.但是现在,当我单击它时,应用程序崩溃了. Visual Studio 2010高亮显示了上面代码中的以下行作为崩溃位置,并显示消息"调用线程无法访问此对象,因为另一个线程拥有该对象.":

System.Windows.Threading.Dispatcher.Run();

堆栈跟踪如下所示:

at System.Windows.Threading.Dispatcher.VerifyAccess()
at MS.Internal.Printing.Win32PrintDialog.ShowDialog()
at System.Windows.Controls.PrintDialog.ShowDialog()
at System.Printing.PrintQueue.GatherDataFromPrintDialog(PrintDialog printDialog, XpsDocumentWriter&amp;amp; writer, PrintTicket&amp;amp; partialTrustPrintTicket, PrintQueue&amp;amp; partialTrustPrintQueue, Double&amp;amp; width, Double&amp;amp; height, String jobDescription)
at System.Printing.PrintQueue.CreateXpsDocumentWriter(String jobDescription, PrintDocumentImageableArea&amp;amp; documentImageableArea)
at System.Windows.Controls.Primitives.DocumentViewerBase.OnPrintCommand()
at System.Windows.Controls.Primitives.DocumentViewerBase.ExecutedRoutedEventHandler(Object target, ExecutedRoutedEventArgs args)
...

我的直觉是,打印对话框正在主UI线程中打开,并试图访问由我自己的线程创建和拥有的文档,从而导致崩溃.

有什么想法可以解决这个问题吗?我想将窗口保留在自己的线程中.

解决方案

再进行一次谷歌搜索后,我偶然发现了以下线程,这似乎是我遇到的确切问题.

PrintDialog和辅助UI线程严重问题

在该线程中,该家伙最终使用了自定义PrintDialog类(其源代码位于此处 ),与内置的PrintDialog几乎相同,但需要进行一些调整以修复这些跨线程错误(并且它也覆盖了XPS Document Writer,显然,XPS Document Writer进一步将其与应用程序的主UI线程联系在一起)

我复制并粘贴了该自定义PrintDialog的代码(并将类重命名为ThreadSafePrintDialog),删除了打印"按钮的CommandTarget,而是使用了自己的Print方法:

private void Print_Executed(object sender, ExecutedRoutedEventArgs args) {
    var printDialog = new ThreadSafePrintDialog();
    if (!printDialog.ShowDialog(this)) return;

    printDialog.PrintDocument(DocumentViewer.Document.DocumentPaginator, "My Document");
}

完美运行.

In my WPF app, I have particular Window which contains, amongst other controls, a DocumentViewer.

When this window is opened and loaded, it dynamically builds a FixedDocument with a progress indicator, and then displays it in the DocumentViewer. It works, and to improve the user experience, I run this window in its own thread, so that the main application window is still responsive while the document is being built.

Based on the tips at this web page, I open my window in a new thread like this:

public void ShowDocumentViewerWindow(params object[] data) {
    var thread = new Thread(() => {
        var window = new MyDocumentViewerWindow(new MyObject(data));
        window.Closed += (s, a) => window.Dispatcher.InvokeShutdown();
        window.Show();
        System.Windows.Threading.Dispatcher.Run();
    });
    thread.SetApartmentState(ApartmentState.STA);
    thread.Start();
}

I have been happy with this setup so far, but I just ran into an issue.

MyDocumentViewerWindow contains a print button, which references the built-in Print command, targeted at the DocumentViewer:

<Button Command="Print" CommandTarget="{Binding ElementName=MyDocumentViewer}">Print</Button>

Before I had the window in its own thread, this worked fine. But now, when I click it, the application crashes. Visual Studio 2010 highlights the following line from the above code as the crash location, with the message 'The calling thread cannot access this object because a different thread owns it.':

System.Windows.Threading.Dispatcher.Run();

The stack trace starts like this:

at System.Windows.Threading.Dispatcher.VerifyAccess()
at MS.Internal.Printing.Win32PrintDialog.ShowDialog()
at System.Windows.Controls.PrintDialog.ShowDialog()
at System.Printing.PrintQueue.GatherDataFromPrintDialog(PrintDialog printDialog, XpsDocumentWriter&amp;amp; writer, PrintTicket&amp;amp; partialTrustPrintTicket, PrintQueue&amp;amp; partialTrustPrintQueue, Double&amp;amp; width, Double&amp;amp; height, String jobDescription)
at System.Printing.PrintQueue.CreateXpsDocumentWriter(String jobDescription, PrintDocumentImageableArea&amp;amp; documentImageableArea)
at System.Windows.Controls.Primitives.DocumentViewerBase.OnPrintCommand()
at System.Windows.Controls.Primitives.DocumentViewerBase.ExecutedRoutedEventHandler(Object target, ExecutedRoutedEventArgs args)
...

My hunch is that the print dialog is opening in the main UI thread, and trying to access the document that is created and owned by my own thread, hence the crash.

Any ideas how I can solve this? I'd like to keep the window in its own thread.

解决方案

After some more Googling, I stumbled across the following thread, which seems to be the exact issue I am having.

PrintDialog and a secondary UI thread severe problem

In that thread, the guy eventually uses a custom PrintDialog class (the source code of which is found here), which is much the same as built-in PrintDialog, but with a few tweaks to fix these cross-thread bugs (and it also overrides the XPS Document Writer, which apparently ties itself even further into the application's main UI thread)

I copied and pasted the code for that custom PrintDialog (and renamed the class to ThreadSafePrintDialog), removed my Print button's CommandTarget, and instead use my own Print method:

private void Print_Executed(object sender, ExecutedRoutedEventArgs args) {
    var printDialog = new ThreadSafePrintDialog();
    if (!printDialog.ShowDialog(this)) return;

    printDialog.PrintDocument(DocumentViewer.Document.DocumentPaginator, "My Document");
}

Works perfectly.

这篇关于在其他UI线程中打印DocumentViewer的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆