后台线程打印问题
我不确定这是否与线程问题有关。
我正在使用标准 Windows 打印机打印标签。标签在 GDI 中绘制,然后传递给另一个对象,该对象在后台线程上打印它。
我在这个过程中使用了BackgroundWorker。 LabelPrinter.Print(Label as PrintLabel)
启动backgroundWorker并将标签详细信息传递给它。 PrintPage 事件处理程序呈现页面并将其发送到打印机。
问题是,用户偶尔会收到一条错误消息:
System.Drawing.Printing.InvalidPrinterException: Settings to access printer '\\printserver\label' are not valid.
at System.Drawing.Printing.PrinterSettings.GetHdevmodeInternal()
at System.Drawing.Printing.PrinterSettings.GetHdevmode(PageSettings pageSettings)
at System.Drawing.Printing.PrintController.OnStartPrint(PrintDocument document, PrintEventArgs e)
at System.Drawing.Printing.StandardPrintController.OnStartPrint(PrintDocument document, PrintEventArgs e)
at System.Drawing.Printing.PrintController.Print(PrintDocument document)
at System.Drawing.Printing.PrintDocument.Print()
用户只是偶尔收到此消息,尽管我被告知连续打印多个标签(标签之间间隔几分钟)情况会更糟。他们将能够整天打印标签,当他们突然遇到此错误并出现 4 或 5 次时,问题通常就会消失。
I am not sure if this is to with a threading issue or not.
I am printing labels to a standard windows printer. The label is drawn in GDI and then passed to another object which prints it on a background thread.
I am using a BackgroundWorker for this process. LabelPrinter.Print(Label as PrintLabel)
starts the backgroundWorker and passes the label details to it. The PrintPage eventhandler renders the page and sends it to the printer.
The issue is, users occasionally get an error stating:
System.Drawing.Printing.InvalidPrinterException: Settings to access printer '\\printserver\label' are not valid.
at System.Drawing.Printing.PrinterSettings.GetHdevmodeInternal()
at System.Drawing.Printing.PrinterSettings.GetHdevmode(PageSettings pageSettings)
at System.Drawing.Printing.PrintController.OnStartPrint(PrintDocument document, PrintEventArgs e)
at System.Drawing.Printing.StandardPrintController.OnStartPrint(PrintDocument document, PrintEventArgs e)
at System.Drawing.Printing.PrintController.Print(PrintDocument document)
at System.Drawing.Printing.PrintDocument.Print()
Users only get this occasionally, although I am informed it is worse when there print multiple labels in succession (few minutes between labels). They will be able to print labels all day, when they will suddenly hit this error and get it 4 or 5 times, then the problem will go usually go away.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是由打印机驱动程序引起的问题。对本机 DocumentProperties() API 函数的调用失败。不幸的是,它没有一个好的方法来准确指示出了什么问题,错误返回仅意味着“它不起作用”。除了标准的“更新驱动程序”方法之外,联系打印机制造商寻求支持将是您最好的选择。他们想确切地知道在呼叫失败时这台打印机还可能发生什么情况。包括打印机状态以及是否有人修改打印机属性。
如果这个异常重复得很好,那么捕获异常就可能是一种黑客攻击。它发生得很早,在打印机完成任何操作之前。睡几秒钟然后重试。永远不要重试。
This is a problem that's induced by the printer driver. The call to the native DocumentProperties() API function failed. Unfortunately it doesn't have a good way to indicate exactly what is wrong, an error return doesn't mean anything more than "it didn't work". Beyond the standard 'update the driver' approach, contacting the printer manufacturer for support would be your best bet. They'll want to know exactly what else might be going on with this printer while the call failed. Including the printer state and whether anybody is tinkering with the printer properties.
If this exception repeats well then catching the exception is a possible hack. It happens very early, before anything is done with the printer. Sleep for a couple of seconds and try again. Don't retry forever.
如果打印机是网络打印机(具有以太网端口),您可能需要尝试直接与该打印机通信,而不是使用打印机共享 (\server\printer)。我所说的直接意思是将其安装为本地tcp/ip打印机。
当我以这种方式与打印机通信时,总是比使用打印机共享更可靠。
If the printer is a network printer (has ethernet port) you may want to try communicating directly with this printer instead of using the printer share (\server\printer). By directly I mean installing it as local tcp/ip printer.
I've always had more reliability when communicating to printers this way rather than using printer shares.