我也被困在AddJob()方法中。随着Windows 10平台的加入,这个问题似乎更加普遍。
在调试中断开执行,堆栈跟踪显示STA线程在调用时被阻塞
的 MS.Internal.PrintWin32Thunk.XpsCompatiblePrinter.JobIdentifier.get 强>
这在一些未知同步对象上的低级WaitOne()上进一步阻塞。
虽然这个问题的细节很薄(这是我在这个主题上发现的唯一一篇文章),但是感谢所接受的解决方案非常好(我已经尝试了各种各样的东西在WPF中打印多年)。
AddJob()在所有方面都完全被替换。甚至可以通过PrintDialog更轻松地控制PrintTicket(设置纸张尺寸,方向,单面/双面打印等)
我似乎无法找到问题。但是这是一种更有前途的打印XPS文件的方法:
public static void PrintXPSToDefaultPrinter(string FilePath) { try { // Create the print dialog object and set options PrintDialog pDialog = new PrintDialog(); pDialog.PageRangeSelection = PageRangeSelection.AllPages; pDialog.UserPageRangeEnabled = true; FileInfo file = new FileInfo(FilePath); XpsDocument xpsDocument = new XpsDocument(FilePath, FileAccess.ReadWrite); FixedDocumentSequence fixedDocSeq = xpsDocument.GetFixedDocumentSequence(); pDialog.PrintDocument(fixedDocSeq.DocumentPaginator, file.Name); } catch (System.IO.IOException ex) { Console.WriteLine("The file is being used by some other process."); } catch (Exception ex) { Console.WriteLine("Exception occured : {0}", ex.Message); } }
您应该像在使用时一样在STA模式下调用它:
static void Main(string[] args) { Thread printingThread = new Thread(() => PrintXPSToDefaultPrinter(@"D:\Assist\RemotePrint\Spool\Donalduck.xps")); printingThread.SetApartmentState(ApartmentState.STA); printingThread.Start(); }
你也可以提一下你想要的任何印花 pDialog.PrintQueue 属性。
pDialog.PrintQueue
希望这可以帮助。享受男人!