System.Printing.PrintQueue QueueStatus 未更新

Mik*_*scu 5 printing wpf system.printing printqueue

有没有办法更新包含在PrintQueue对象中的打印队列状态信息?

我试过在PrintQueue对象上调用Refresh,但这并没有真正做任何事情。例如,我已经关闭了打印机,并且控制面板正确地将打印机显示为“离线”,但是QueueStatus属性以及 IsOffline 属性并未反映这一点 - 无论我在两者上调用 Refresh 多少次有问题的PrintServerPrintQueue

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

Pét*_*égi 0

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

第一步:初始化您的 printDocument。

第二步:获取您的打印机名称 System.Drawing.Printing.PrinterSettings.InstalledPrinters.Cast<string>();

并将其复制到您的printerDocument.PrinterSettings.PrinterName

第三步:尝试打印并处理。

printerDocument.Print();
printerDocument.Dispose();
Run Code Online (Sandbox Code Playgroud)

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

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

这是实现:

        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;    
        }
Run Code Online (Sandbox Code Playgroud)

祝你好运。