In my project method PrintToPrint for crystal report works fine but after installation of the software when Printer is not connected to machine this method makes my software process hangs. Can any one help me solve this issue?

1

There are 1 best solutions below

1
On

My solution would be to write a function to confirm that the printer is online before making the call to open a crystal report.

using System.Management;

public bool IsPrinterReady(string printerName)
        {

        bool bprinterOnline = false;       

        ManagementScope scope = new ManagementScope(@"\root\cimv2");
        scope.Connect();

        // Select Printers from WMI Object Collections
        ManagementObjectSearcher printerSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_Printer");

        foreach (ManagementObject printer in printerSearcher.Get())
        {

            if (string.IsNullOrEmpty(printer["Name"].ToString()))
            {
                if (printer["Name"].ToString().ToLower().Equals(printerName.ToLower()))
                {

                    switch (printer["WorkOffline"].ToString().ToLower())
                    {
                        case "true":
                            bprinterOnline= true;
                            break;
                        case "false": 
                            bprinterOnline= false;
                            break;
                        default:
                            bprinterOnline= false;
                            break;
                    }
                    break;                     
                }
            }
        }
        return bprinterOnline;
    }