1

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

 ProcessStartInfo startInfo = new ProcessStartInfo("c:\\windows\\system32\\test.exe");
 String s = " ";

 startInfo.WindowStyle = ProcessWindowStyle.Hidden;
 startInfo.CreateNoWindow = true;
 startInfo.RedirectStandardInput = true;
 startInfo.RedirectStandardOutput = true;
 startInfo.UseShellExecute = false;
 Process p = Process.Start(startInfo);
 p.StandardInput.WriteLine("list volume\n");
 String f = "";
 bool ignoredHeader = false;

 s = p.StandardOutput.ReadLine();
 p.WaitForExit();

Please help me

sidon
  • 1,434
  • 1
  • 17
  • 30
user1351166
  • 11
  • 1
  • 3
  • 2
    Perhaps the program doesn't output anything sometimes? – CodesInChaos Apr 23 '12 at 11:26
  • Or does it output more than one line sometimes? In that case the called program will block once the output buffer is full. And since you never read from it after the first line, it will block forever, and thus `WaitForExit()` won't return. In short, your program is wrong, unless the called program always outputs exactly one line, no more, no less. – CodesInChaos Apr 23 '12 at 11:27
  • Or perhaps the program writes to standard error instead of standard output sometimes? For example, when it detects that something went wrong? – Jon Apr 23 '12 at 11:28
  • @Arion it just hang the application – user1351166 Apr 23 '12 at 11:32
  • @CodeInChaos no it is not giving any output – user1351166 Apr 23 '12 at 11:32
  • Is the program waiting for another line of input after it processes your `list volume`? If so, it might not flush its output buffer after the first line of output; you might need to give it another command (something like `exit`) to tell it to exit and flush its stdout. – Joe White Apr 23 '12 at 11:35

1 Answers1

2

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.

Community
  • 1
  • 1
CodesInChaos
  • 106,488
  • 23
  • 218
  • 262