StreamReader を使用してプロセスから出力データを読み取ろうとしていますが、StreamReader がブロックされ、出力が返されません。
私のプロセスは次のようになります。
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.Arguments = args;
startInfo.FileName = filename;
StartInfo.WorkingDirectory = aDirectory;
StartInfo.UseShellExecute = false;
StartInfo.RedirectStandardOutput = true;
Process p = new Process();
p.StartInfo = startInfo;
p.Start();
StreamReader はその後すぐに呼び出されます。
StreamReader strmRead = p.StandardOutput;
char[] output = new char[4096];
while(true){
strmRead.Read(output,0,output.Length);
string outputString = new string(output);
Debug.WriteLine(outputString);
}
コードは、Read メソッドの呼び出しでハングします。プログラムを手動で強制終了すると、プロセスからの出力がデバッグ コンソールに書き込まれます。プロセス出力は改行文字を使用しないため、Process.OutputDataReceived を使用しても機能しません。無期限にブロックすることなく、ストリームからプロセス出力を取得するにはどうすればよいですか?
編集:私がすでに得た答えを考えると、コードに問題があるのではなく、プロセスが標準出力を放棄したり、出力をフラッシュしたりしないという問題のようです。他の誰かが洞察を持っている場合は、お気軽にコメントしてください。