Printing the content of a DocumentViewer in a different UI thread
Asked Answered
L

2

5

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.

Laflamme answered 7/9, 2011 at 14:22 Comment(0)
L
7

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.

Laflamme answered 7/9, 2011 at 15:7 Comment(2)
can you please put the class here, because the link is dead, I am really struggling with this issue and I think we have exactly the same problemLindsy
I mean can you please put the source code of the ThreadSafePrintDialog classLindsy
V
1

Your hunch is correct. You cannot access this object on the UI thread when it has been created by another thread.

I believe you have a few options:

  1. You could create this document on the UI thread, perhaps gather the information that you need in a background thread and then actually construct the object on the UI thread. It depends what your document creation entails. You could do something like:

    public void CreateDocument(T inputDataForDocumentCreation) {
      var uiDispatcher = Dispatcher.CurrentDispatcher;
      ThreadPool.QueueUserWorkItem(_ => {
        // Load and create document components with yourDataForDocumentCreation
    
         dispatcher.BeginInvoke(DispatcherPriority.Normal, () => {
         //Actually create the document (this will happen on the UI thread, so it may be accessed from the UI thread)
      });
      });
    }
    
  2. You could perhaps send this command to the thread that creates this other document? Hold on to this thread and do a thread.Invoke(printMethod)

  3. You could look into Freezable Objects. Look at the bottom of this page, heading "Creating Your Own Freezable Class". This would make your document thread-safe to access from a different thread than that which created it.

Vincenz answered 7/9, 2011 at 14:34 Comment(1)
Thanks for that. Creating my document involves instantiating a FixedDocument, adding FixedPage objects, filling them with controls, etc. Because the FixedDocument is a DispatcherObject, I could not create it in a background thread and then set it as source for the DocumentViewer, as doing so also created a cross-thread violation. I found I have to create my document in the same thread as my DocumentViewer - i.e. on a UI thread :-( But I did just find a workaround to my issue - I'll post it now.Laflamme

© 2022 - 2024 — McMap. All rights reserved.