开发者

System.Printing.PrintQueue QueueStatus not updating

开发者 https://www.devze.com 2023-01-26 08:57 出处:网络
Is there 开发者_运维问答a way to update the print queue status information contained in the PrintQueue object?

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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号