0

I've recently asked several related questions to this issue but this time I want to get as straight to the point as possible with the concern I am having in my development.

We are trying to print tickets through a mobile printer and need to determine if the printer is online, offline or if there's an error so we can better handle it: enter image description here

Here again is a snippet of my code when I print but can't seem to get it to trap if any issues occur:

            Process process = new Process();
            //process.StartInfo.CreateNoWindow = true;
            process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            process.StartInfo.FileName = defFile;
            if (rwPrinter.Length > 0)
            {
                process.StartInfo.Verb = "printto";
                //process.StartInfo.Verb = (Path.Combine(System.Windows.Forms.Application.StartupPath, "printto.exe"));
                process.StartInfo.Arguments = "\"" + rwPrinter + "\"";
            }
            else
            {
                process.StartInfo.Verb = "print";
            }

            try
            {
                process.Start();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

Pardon my hastiness on this as I have a boss that is impatient with me to get this working as desired, and please advise on how I can get this to error trap, thanks.

jfalberg
  • 141
  • 4
  • 16
  • Have you taken a look at the [example shown on MSDN](http://msdn.microsoft.com/en-us/library/aa970845.aspx)? This uses the System.Printing.PrintServer class to determine a given print server's status. – SWalters Nov 01 '13 at 17:40
  • Also have a look at the [PrinterSettings class](http://msdn.microsoft.com/de-de/library/system.drawing.printing.printersettings%28v=vs.110%29.aspx) for printer status information. – keenthinker Nov 01 '13 at 17:43
  • In the PrinterSettings example, I noticed it was attempting to print a text document before determining a setting. Is there a way not to test like that? – jfalberg Nov 01 '13 at 18:07

1 Answers1

0

For after the fact handling of issues you can wait for exit and then check the ExitCode. If the process that handles the printing is so nice to report back a meaningfull code you can show more meaningfull messages but that depends on the process being called.

try
{
      process.Start();
      process.WaitForExit();
      if (process.ExitCode >0)
      {
          MessageBox.Show(String.Fromat("print failed, exit code :{0} ", process.ExitCode ));
      }
}
catch (Exception ex)
{
      MessageBox.Show(ex.Message);
}
rene
  • 41,474
  • 78
  • 114
  • 152
  • While I was very hopeful this would solve my problem, when I tried to debug this and purposefully turned my mobile printer offline and eventually online, it looked like it was stuck on the "process.WaitForExit()" line somehow. – jfalberg Nov 01 '13 at 18:05
  • Which process does the printing? – rene Nov 01 '13 at 18:25
  • It's a "Print" process. Looks like my question is similar to the following unanswered question at http://stackoverflow.com/questions/14455964/c-sharp-printer-properties-wmi – jfalberg Nov 01 '13 at 19:01
  • This is more what you need but than in a c# implementation: http://support.microsoft.com/kb/160129 – rene Nov 01 '13 at 19:34
  • Hmmm, one thing I noticed is after I attempt to print to an offline printer, the PrinterStatus value changes from 3 to 2 until I manually delete from the Printer queue. I would prefer something that checks before attempting to print. If I'm stuck with this way I may have to programmatically delete this print job from the queue somehow. – jfalberg Nov 01 '13 at 19:55