System.Printing.PrintQueue QueueStatus 未更新

发布于 2024-10-04 14:26:16 字数 1176 浏览 2 评论 0原文

有没有办法更新 PrintQueue 对象?

我尝试致电 刷新 PrintQueue 对象,但这并没有真正做任何事情。例如,我关闭了打印机,并且控制面板正确地将打印机显示为“脱机”,但是 QueueStatus 属性以及 IsOffline 属性都没有反映这一点 - 无论我在两个队列上调用 Refresh 多少次打印服务器 和有问题的 PrintQueue

我已经看到了如何使用 WMI 查询获取状态信息的示例,但我想知道 - 因为这些属性可在 PrintQueue 对象 - 是否有任何方法可以使用它们。

Is there a way to update the print queue status information contained in the PrintQueue object?

I've tried calling Refresh on the PrintQueue object but that doesn't really do anything. For instance, I've turned off the printer and the Control Panel correctly shows the printer as "Offline", however the QueueStatus property, as well as the IsOffline property don't reflect that - no matter how many times I call Refresh on both the PrintServer and the PrintQueue in question.

I've seen examples of how to get status information using WMI queries but I wonder - since these properties are available on the PrintQueue object - whether there is any way to use those.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不寐倦长更 2024-10-11 14:26:16

尝试打印 PrintDocument (System.Drawing.Printing) 后,尝试检查打印作业的状态。

第一步:
初始化您的打印​​文档。

第二步:
从 System.Drawing.Printing.PrinterSettings.InstalledPrinters.Cast(); 获取打印机名称

并将其复制到 printerDocument.PrinterSettings.PrinterName

第三步:
尝试打印并丢弃。

printerDocument.Print();
printerDocument.Dispose();

最后一步:在任务中运行检查(不要阻塞 UI 线程)。

   Task.Run(()=>{
     if (!IsPrinterOk(printerDocument.PrinterSettings.PrinterName,checkTimeInMillisec))
     {
        // failed printing, do something...
     }
    });

这是实现:

        private bool IsPrinterOk(string name,int checkTimeInMillisec)
        {
            System.Collections.IList value = null;
            do
            {
                //checkTimeInMillisec should be between 2000 and 5000
                System.Threading.Thread.Sleep(checkTimeInMillisec);

                using (System.Management.ManagementObjectSearcher searcher = new System.Management.ManagementObjectSearcher("SELECT * FROM Win32_PrintJob WHERE Name like '%" + name + "%'"))
                {
                    value = null;

                    if (searcher.Get().Count == 0) // Number of pending document.
                        return true; // return because we haven't got any pending document.
                    else
                    {
                        foreach (System.Management.ManagementObject printer in searcher.Get())
                        {
                            value = printer.Properties.Cast<System.Management.PropertyData>().Where(p => p.Name.Equals("Status")).Select(p => p.Value).ToList();
                            break; 
                        }
                    }
                }
           }
           while (value.Contains("Printing") || value.Contains("UNKNOWN") || value.Contains("OK"));

           return value.Contains("Error") ? false : true;    
        }

祝你好运。

After try to print your PrintDocument (System.Drawing.Printing), try to check status of printjobs.

First step:
Initialize your printDocument.

Second step:
Get your printer Name From System.Drawing.Printing.PrinterSettings.InstalledPrinters.Cast<string>();

And copy it into your printerDocument.PrinterSettings.PrinterName

Third step:
Try to print and dispose.

printerDocument.Print();
printerDocument.Dispose();

Last step: Run the check in a Task (do NOT block UI thread).

   Task.Run(()=>{
     if (!IsPrinterOk(printerDocument.PrinterSettings.PrinterName,checkTimeInMillisec))
     {
        // failed printing, do something...
     }
    });

Here is the implementation:

        private bool IsPrinterOk(string name,int checkTimeInMillisec)
        {
            System.Collections.IList value = null;
            do
            {
                //checkTimeInMillisec should be between 2000 and 5000
                System.Threading.Thread.Sleep(checkTimeInMillisec);

                using (System.Management.ManagementObjectSearcher searcher = new System.Management.ManagementObjectSearcher("SELECT * FROM Win32_PrintJob WHERE Name like '%" + name + "%'"))
                {
                    value = null;

                    if (searcher.Get().Count == 0) // Number of pending document.
                        return true; // return because we haven't got any pending document.
                    else
                    {
                        foreach (System.Management.ManagementObject printer in searcher.Get())
                        {
                            value = printer.Properties.Cast<System.Management.PropertyData>().Where(p => p.Name.Equals("Status")).Select(p => p.Value).ToList();
                            break; 
                        }
                    }
                }
           }
           while (value.Contains("Printing") || value.Contains("UNKNOWN") || value.Contains("OK"));

           return value.Contains("Error") ? false : true;    
        }

Good luck.

帅气称霸 2024-10-11 14:26:16

很晚的答案,

到目前为止我发现的唯一方法是重新启动打印后台处理程序服务,但这会导致 WMI 出现一系列其他问题,而且这不是一个干净的解决方案。

Very late answer,

The only way I found so far is restarting the print Spooler service but this causes a bunch of other problems with WMI and it's not a clean solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文