0

I'm using wkhtmltopdf to generate some pdfs from a asp.net mvc site. I'm using code found on another SO question here. There is a problem with this code in that there is a hard coded 60 second wait for the process to complete. I think it would be much improved if we could simply have a trigger or signal to watch for then exit and read the output. So does anyone know of a way to listen for a response from the wkhtmltopdf process?

 // read the output here...
string output = p.StandardOutput.ReadToEnd(); 

// ...then wait n milliseconds for exit (as after exit, it can't read the output)
p.WaitForExit(60000); 

// read the exit code, close process
int returnCode = p.ExitCode;
p.Close(); 
Community
  • 1
  • 1
NullReference
  • 4,404
  • 12
  • 53
  • 90

1 Answers1

2

I'm not sure that's doing what you think it is. It will exit as soon as it's complete. the 60 seconds is a maximum wait time for it to complete.

As stated in on MSDN

The WaitForExit(Int32) overload is used to make the current thread wait until the associated process terminates. This overload instructs the Process component to wait a finite amount of time for the process to exit. If the associated process does not exit by the end of the interval because the request to terminate is denied, false is returned to the calling procedure. You can specify a negative number (Infinite) for milliseconds, and Process.WaitForExit(Int32) will behave the same as the WaitForExit overload. If you pass 0 (zero) to the method, it returns true only if the process has already exited; otherwise, it immediately returns false.

(emphasis mine)

Nathan
  • 1,080
  • 7
  • 16