You can use Console.KeyAvailable
property to check if there is a symbol available in the input stream. However you should use Console.ReadKey
method in conjunction with KeyAvailable
because ReadLine
will block until the stdin
receives newline sequence or end-of-file.
Check if an IO stream is blocked in C#
-
29-09-2022 - |
문제
In C#
, one can run a process and interact with it using its stdin
and stdout
.
Interactive programs however sometimes block waiting for input. The stream is not closed, and when calling the ReadLine
method, the method blocks until more data becomes available.
Is there a way to verify if there is currently still data available on the stdin without blocking (if no more data is available, the program must feed the stdin of the process)...
Code example:
this.process = new Process ();
this.process.StartInfo.FileName = "foo";
this.process.StartInfo.Arguments = "--nowarnings -i";
this.process.StartInfo.UseShellExecute = false;
this.process.StartInfo.RedirectStandardInput = true;
this.process.StartInfo.RedirectStandardOutput = true;
this.process.StartInfo.RedirectStandardError = true;
this.process.StartInfo.CreateNoWindow = true;
this.process.Start ();
this.stdin = this.process.StandardInput;
this.stdout = this.process.StandardOutput;
this.stdin.WriteLine ("command1");
while (!stdout.EndOfStream) {
Console.WriteLine (stdout.ReadLine ());
}
this.stdin.WriteLine ("command2");
해결책
제휴하지 않습니다 StackOverflow