gpt4 book ai didi

wpf - 多线程 wpf 窗口中的 Printdialog 抛出 TargetInvocationException

转载 作者:行者123 更新时间:2023-12-04 14:37:28 25 4
gpt4 key购买 nike

我设计了一个多线程应用程序,它在像 this 这样的专用线程中启动大多数窗口。 :

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

但是,如果在这些窗口中的一个线程中,我尝试通过简单地调用来打印一些东西
PrintDialog pDialog = new PrintDialog();
bool? doPrint = pDialog.ShowDialog();

我得到一个 TargetInvocationException - 它看起来 PrintDialog 与我的窗口不在同一个线程中。

有没有办法创建一个线程不可知(或“线程保存”)的 PrinterDialog ?

最佳答案

答案是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



有人构建了一个启用线程的 PrintDialog here .这似乎是使打印在启用线程的应用程序中工作的唯一方法。

(我很想在这里重复代码,但它超过了答案的最大长度)

评论:
  • 系统图
  • System.Printing
  • System.Windows.Forms

  • 需要引用。

    关于wpf - 多线程 wpf 窗口中的 Printdialog 抛出 TargetInvocationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2972513/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com