StandardOutput.ReadLine() hang the application using c#

前端 未结 1 1831
借酒劲吻你
借酒劲吻你 2021-01-27 00:52

Following is the application code. some time p.StandardOutput.ReadLine(); works fine but some time it hang up i tried all things but still getting this error

 Pr         


        
相关标签:
1条回答
  • 2021-01-27 01:40

    Your program is correct if the called program always outputs exactly one line, and that line is shorter than the buffer employed by the system.

    If it doesn't output a line, ReadLine won't return. So your program is broken in this case.

    If it outputs too much, the output buffer runs full, and the called program will block on its Write call, until somebody reads enough from the output. Since you never read from the output buffer beyond the first line, this block will last forever, and thus the called program will never terminate. This in turn causes your program to deadlock at p.WaitForExit().

    The documentation clearly states:

    Do not wait for the child process to exit before reading to the end of its redirected stream.

    The code example avoids a deadlock condition by calling p.StandardOutput.ReadToEnd before p.WaitForExit. A deadlock condition can result if the parent process calls p.WaitForExit before p.StandardOutput.ReadToEnd and the child process writes enough text to fill the redirected stream. The parent process would wait indefinitely for the child process to exit. The child process would wait indefinitely for the parent to read from the full StandardOutput stream.

    0 讨论(0)
提交回复
热议问题