views:

71

answers:

1

I have designed a multithreaded app, which is starting most windows in an dedicated thread like this:

Thread newWindowThread = new Thread(new ThreadStart(ThreadStartingPoint));
newWindowThread.SetApartmentState(ApartmentState.STA);
newWindowThread.IsBackground = true;
newWindowThread.Start();

However, if in one of those window-in-own-thread I try to print something by simply calling

PrintDialog pDialog = new PrintDialog();
bool? doPrint = pDialog.ShowDialog();

I get a TargetInvocationException - it does look like the PrintDialog does not reside in the same thread as my window.

Is there any way to create a thread-agnostic (or "thread-save") PrinterDialog ?

A: 

The answer is here:

PrintDialog uses a private class Win32PrintDialog which seems to access Application.Current.MainWindow in order to get the parent HWND window handle to use as a native print dialog window parent

Someone built a Thread-Enabled PrintDialog here. This seems to be the only way to get Printing working in an Thread-Enabled app.

(I'd loved to repeat the code here, but it exceeds the maximum length of answers)

Remark:

  • System.Drawing
  • System.Printing
  • System.Windows.Forms

are needed references.

Nils