4

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?

Munavvar
  • 802
  • 1
  • 11
  • 33
  • Also know that if you try manually opening a console and execute your command, if the console immediately gives you back a prompt even if the program you executed keeps running, then your WaitForExit code will only wait for the `cmd` process (the console) to exit, not that other program. – Lasse V. Karlsen Nov 23 '16 at 12:14
  • Additionally, you're not escaping backslashes so this: `"E:\p...` should likely be `@"E:\p...`. – Lasse V. Karlsen Nov 23 '16 at 12:15
  • Thanks for reply @LasseV.Karlsen. but I am not getting any error and print process is done without any error. – Munavvar Nov 23 '16 at 12:16
  • 2
    Then the problem is that you're waiting for the console process to return, and not the print process. – Lasse V. Karlsen Nov 23 '16 at 12:17
  • 2
    Use start /wait to tell cmd.exe to not complete until the process is completed. Or just don't use cmd.exe at all since it doesn't do anything useful. A program like this will likely just dump the print job in the spooler and is not going to wait for it to be printed. You'd have to tackle System.Printing.PrintQueue, not recommended. – Hans Passant Nov 23 '16 at 12:26
  • thats Right @LasseV.Karlsen, but is there any way that notify me when printing process is done?. – Munavvar Nov 23 '16 at 12:30
  • Either by *finding* the printing process and waiting for it to exit, or simply execute the print process directly. – Lasse V. Karlsen Nov 23 '16 at 12:31
  • you have wrote "I have created Print spooler application to print pdf asynchronously.". - Can you paste the portion of code that is executed in the pdfprint.exe? - I don't know if the veryPDF has any events such as EndPrint like the [PrintDocument](https://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument.aspx) class. Can you use PrintDocument instead? - I assume that the file is sent to the print queue and exit because it is a fire and forget process I hope it helps:) – Sotiris Mar 23 '17 at 12:23

3 Answers3

8

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.

Community
  • 1
  • 1
Robert Petermeier
  • 4,122
  • 4
  • 29
  • 37
0

Here's a If-Everything-Else-Fails approach you might have to take (if pdfprint.exe doesn't wait until completion for termination). It's ugly, but it'll work:

  1. Enter a while loop until the destination file exists (ideally with a timeout, in case pdfprint.exe runs into a problem and never generates the file)
  2. Enter a second while loop, where every X milliseconds (say, 500? 1000?), it checks the file length of the output PDF. If the file size is the same between checks, you assume the output process is finished and begin working with the file.

Like I said, it's not pretty (and you shouldn't use it unless you have no recourse)... but it gets the job done. I've had to use this sort of approach for handling incoming files being copied over the network (you need to process files when they come in, but you need to know that the file is finished being copied before touching it, because PDFs will lock as soon as they're opened and then the file-copy will fail.)

Kevin
  • 2,133
  • 1
  • 9
  • 21
0

Have you tried this?

procStartInfo.LoadUserProfile = true;

Pedro Luz
  • 973
  • 5
  • 14