Monday, September 29, 2014

Getting the output when running a Process

Hello z0802361,


"Once process starts, if try to redirect output and get the text it will hang"

How you did this in your code? You may need to read the MSDN article below to see how you can do this:


http://ift.tt/LyDsNX


"The redirected StandardOutput stream can be read synchronously or asynchronously. Methods such as Read , ReadLine , and ReadToEnd perform synchronous read operations on the output stream of the process. These synchronous read operations do not complete until the associated Process writes to its StandardOutput stream, or closes the stream.


In contrast, BeginOutputReadLine starts asynchronous read operations on the StandardOutput stream. This method enables a designated event handler for the stream output and immediately returns to the caller, which can perform other work while the stream output is directed to the event handler.


Synchronous read operations introduce a dependency between the caller reading from the StandardOutput stream and the child process writing to that stream. These dependencies can result in deadlock conditions. When the caller reads from the redirected stream of a child process, it is dependent on the child. The caller waits on the read operation until the child writes to the stream or closes the stream. When the child process writes enough data to fill its redirected stream, it is dependent on the parent. The child process waits on the next write operation until the parent reads from the full stream or closes the stream. The deadlock condition results when the caller and child process wait on each other to complete an operation, and neither can proceed. You can avoid deadlocks by evaluating dependencies between the caller and child process."


Possible fix: The code example avoids a deadlock condition by calling p.StandardOutput.ReadToEnd before p.WaitForExit


Best regards,







Barry

We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.

Click HERE to participate the survey.


No comments:

Post a Comment