Search code examples
c#printingthermal-printer

Using ManagementObjectSearcher and Win32_Printer to check Status


I have been forced to use the RawPrinterHelper class to print to a thermal printer because the PrintDocument method has proven unfit for POS printing.

I now need to check the status of the printer prior to printing to be sure that it is, online and ready to print. I have been able to successful check attributes from Win32_Printer. I can see properties such as PrinterStatus, changing from 3, to a 2 or a 1 when out of paper or tray is open. This is great.

My question is, which properties should indicate it is O.K. to print? There must be more than just checking if PrinterStatus is idle (3).

    private bool ReadyCheck(string printerName)
    {
        bool ready = false;
        string query = string.Format("SELECT * from Win32_Printer WHERE Name LIKE '%{0}'", printerName);

        using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(query))
        using (ManagementObjectCollection coll = searcher.Get())
        {
            try
            {
                foreach (ManagementObject printer in coll)
                {
                    // Print status = 3, idle and ready to print
                    string status = printer.Properties["PrinterStatus"].Value.ToString();
                    string extendedPrinterStatus = printer.Properties["ExtendedPrinterStatus"].Value.ToString();

                    // What else means printer is ready?
                    if (status.Trim() == "3")
                        ready = true;

                }
            }
            catch (ManagementException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        return ready;

    }

Edit: @vendettamit Sorry I didn't ask very well. For instance, Idle (3) indicates it's OK to print. I am wondering if there are more which also indicate it's OK to print such as; Printing (4), WarmUp (5), ect, or is Idle (3) the only time you should send the next print job? Thanks


Solution

  • Below are the values that you can take into account to check if It's ready to print:

    Other (1)
    Unknown (2)
    Idle (3) 
    Printing (4)
    Warmup (5)
    Stopped Printing (6)
    Offline (7)
    

    Also a note from Remarks on MSDN documentation for Win32_Printer-

    If you are retrieving PrinterStatus = 3 or PrinterState = 0, the printer driver may not be feeding accurate information into WMI. WMI retrieves the printer information from the spoolsv.exe process. It is possible the printer driver does not report its status to the spooler. In this case, Win32_Printer reports the printer as Idle.