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

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

问题描述

在我的 WPF 应用程序中,我有一个特定的 Window,其中包含一个 DocumentViewer.

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

基于 上的提示这个网页,我在一个像这样的新线程中打开我的窗口:

public void ShowDocumentViewerWindow(params object[] data) {var 线程 = 新线程(() => {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);线程.开始();}

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

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

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

在我将窗口放在自己的线程中之前,这很好用.但是现在,当我单击它时,应用程序崩溃了.Visual Studio 2010 将上述代码中的以下行突出显示为崩溃位置,并显示消息调用线程无法访问此对象,因为其他线程拥有它.":

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

堆栈跟踪是这样开始的:

在 System.Windows.Threading.Dispatcher.VerifyAccess()在 MS.Internal.Printing.Win32PrintDialog.ShowDialog()在 System.Windows.Controls.PrintDialog.ShowDialog()在 System.Printing.PrintQueue.GatherDataFromPrintDialog(PrintDialog printDialog, XpsDocumentWriter&amp; writer, PrintTicket&amp;amp; partialTrustPrintTicket, PrintQueue&amp;amp; partialTrustPrintQueue, Double&amp;amp; width, Double&amp;amp;高度,字符串作业描述)在 System.Printing.PrintQueue.CreateXpsDocumentWriter(String jobDescription, PrintDocumentImageableArea&amp;amp; documentImageableArea)在 System.Windows.Controls.Primitives.DocumentViewerBase.OnPrintCommand()在 System.Windows.Controls.Primitives.DocumentViewerBase.ExecutedRoutedEventHandler(Object target, ExecutedRoutedEventArgs args)...

我的预感是打印对话框在主 UI 线程中打开,并试图访问由我自己的线程创建和拥有的文档,因此崩溃.

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

解决方案

经过更多的谷歌搜索后,我偶然发现了以下线程,这似乎正是我遇到的问题.

PrintDialog 和辅助 UI线程严重问题

在那个线程中,这家伙最终使用了一个自定义的 PrintDialog 类(其源代码可以在 here),这与内置的 PrintDialog 非常相似,但进行了一些调整以修复这些跨线程错误(并且它还覆盖了 XPS 文档编写器,它显然与应用程序的主 UI 线程更紧密地联系在一起)

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

private void Print_Executed(object sender, ExecutedRoutedEventArgs args) {var printDialog = new ThreadSafePrintDialog();如果 (!printDialog.ShowDialog(this)) 返回;printDialog.PrintDocument(DocumentViewer.Document.DocumentPaginator, "我的文档");}

完美运行.

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天全站免登陆