Click here to Skip to main content
15,913,669 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
I've an application the needs to query the state of a printer before attempting to send it anything. If the status of the printer is deemed not valid, the job is sent to a seconday printer (after that printer status is also checked). The user is not displayed a print dialog to select a printer prior to printing, but the destination of various reports with primary and secondary printers are set-up in the application config.

The following code is used to query the state of a named printer, and has been working fine for the past 8-9 months. Placing some trace timings around the routine, it's been executing well under a second to query the printer status and return the result.

C#
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Printer"))
{
  ManagementObject moPrinter = null;
  foreach (ManagementObject mo in searcher.Get())
    if (mo["Name"].ToString().ToLower() == printerName.ToLower())
    {
      moPrinter = mo;
      break;
    }
  if (moPrinter != null)
  {
    int status = Int32.Parse(moPrinter["PrinterStatus"].ToString());
    result = !((status == 1) || (status == 7));
  }
}


However, my client has since called me to say that printing a letter to their client after processing an order is taking 30-60 seconds to complete, therefore drastically reducing their productivity. Installing a version of my application with trace timings highlights the following routine is now taking around 20-30 seconds to check the printer status using WMI, rather than under a second.

I've not changed the code, so what's caused this increased delay in getting WMI results? I beleive it's their network, but where do I start as I'm a developer not a network guy. Should I change this routine? What can I check or get their IT guys to check? Does user permission have an effect (their using XP)? Does adding new printers or specific network software have an effect? Any services that should/should not be running?

Any help or guidance would be appreciated.

Andrew :confused:
Posted

1 solution

I don't know what might cause the problem you mentioned, but you may try to search for the specific printer object instead of all printers.

The query SELECT * FROM Win32_Printer WHERE Name = 'pRinTeR' also returns the printer named "Printer" oder "PRINTER".
You don't need to fetch all printers, let WMI do the work.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900