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; writer, PrintTicket&amp; partialTrustPrintTicket, PrintQueue&amp; partialTrustPrintQueue, Double&amp; width, Double&amp; height, String jobDescription)
at System.Printing.PrintQueue.CreateXpsDocumentWriter(String jobDescription, PrintDocumentImageableArea&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.
See Question&Answers more detail:
os