Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Process WaitForExit not waiting

I have created Print spooler application to print pdf asynchronously.

(Application uses veryPDF command to print from network printer)

Here is Code

   var procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", " /c" + "E:\pdfprint_cmd\pdfprint.exe -$ 388444444448350FA394 E:\PrintSpoolerApplication\PrintSpoolerApplication\bin\Debug\45940.pdf");
   procStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
   procStartInfo.Verb = "runas";
   procStartInfo.UseShellExecute = false;
   procStartInfo.CreateNoWindow = true;
   var proc = new System.Diagnostics.Process();
   proc.StartInfo = procStartInfo;
   proc.Start();
   proc.WaitForExit();

// Some stuff

But It did not Wait on WaitForExit code. It did executing (here //Some stuff) even if my document is in printer queue.

Is there any other way that notify when printing is done?

like image 600
Munavvar Avatar asked Feb 06 '23 15:02

Munavvar


1 Answers

Your code waits for cmd.exe to finish, which (probably) terminates immediately after it has started pdfprint.exe as a child process. I suggest you

  • either start pdfprint.exe directly (why do you need the Windows command line here anyway?)
  • or find the Process object of the child process -- e.g. through WMI, as described here -- and wait for that process to exit instead.

However, both approaches only work if pdfprint.exe actually waits for the scheduled print job to be completed. I don't know the tool, so I have no idea if it behaves that way. If it doesn't, you would have to access the print queue, which (as pointed out by Hans in his comment) is not recommended.

like image 129
Robert Petermeier Avatar answered Feb 08 '23 06:02

Robert Petermeier