System.Printing.PrintQueue QueueStatus does not update

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

I tried calling Refresh on PrintQueue , but it actually does nothing. For example, I turned off the printer and the control panel correctly displays the printer as "stand-alone," however the QueueStatus property and the IsOffline property do not reflect this - no matter how many times I call Refresh on PrintServer and PrintQueue .

I saw examples of getting status information using WMI queries, but I am wondering - since these properties are available on the PrintQueue object - is there a way to use them.

+3
source share
1 answer

After trying to print to PrintDocument (System.Drawing.Printing), try checking the status of printjobs.

First step: Initialize your print.ocument document.

Second Step: Get Your Printer Name From System.Drawing.Printing.PrinterSettings.InstalledPrinters.Cast<string>();

And copy it to your printerDocument.PrinterSettings.PrinterName

Third step: Try to print and delete.

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

Last step: run a task check (DO NOT block the user interface 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
source

Source: https://habr.com/ru/post/1776581/


All Articles