2011-06-21 15 views
2

ProcessStartInfoおよびProcessプログラムを起動して(getdiff.exeを実行して)、プログラムが生成するすべての出力を読み込みたいと思います。後でもっと建設的な方法でデータを使用します。データを印刷して確実に機能させるだけです。しかし、プログラムは終了する必要はありません。なぜ誰がそれを知っていますか?進んでいただきありがとうございます。これにプログラムを変更するプロセスを使用しているときにプログラムが終了しない

ProcessStartInfo psi = new ProcessStartInfo("getdiff.exe"); 
psi.Arguments = "DIFF"; 
psi.UseShellExecute = false;     
psi.RedirectStandardInput = true; 
psi.WorkingDirectory = "c:\\test"; 

Process p = Process.Start(psi); 
string read = p.StandardOutput.ReadToEnd(); 
p.WaitForExit(); 

Console.WriteLine(p); 
Console.WriteLine("Complete"); 

p.Close(); 

正しく動作してそれを得た:それは関連だ

ProcessStartInfo psi = new ProcessStartInfo("getdiff.exe"); 
psi.Arguments = "DIFF"; 
psi.UseShellExecute = false;     
psi.RedirectStandardInput = true; 
psi.WorkingDirectory = "c:\\test"; 

Process p = Process.Start(psi); 
StreamReader read = p.StandardOutput; 

while (read.Peek() >= 0) 
    Console.WriteLine(read.ReadLine()); 

Console.WriteLine("Complete"); 
p.WaitForExit(); 
p.Close(); 
+0

このコードを試してみてください?あなたのアプリケーション、または呼び出されたプロセス? – Jaymz

+0

なぜ終了するのですか?コンソールアプリケーションですか?他に何か? –

+0

これは大きなプログラムの関数です。しかし、これだけを実行すると、プログラムは決して終了しません。それは健康ではありません。 – Teletha

答えて

2
ProcessStartInfo psi = new ProcessStartInfo("getdiff.exe"); 
psi.Arguments = "DIFF"; 
psi.UseShellExecute = false;     
psi.RedirectStandardInput = true; 
psi.WorkingDirectory = "c:\\test"; 

Process p = Process.Start(psi); 
StreamReader read = p.StandardOutput; 

while (read.Peek() >= 0) 
    Console.WriteLine(read.ReadLine()); 

Console.WriteLine("Complete"); 
p.WaitForExit(); 
p.Close(); 
1

わからない場合がありますが、結果のストリームで何かをやってなくてpsi.RedirectStandardInput = true;を行います。何らかの形で、入力ストリームが終了する前に入力ストリームが「閉じている」必要がありますか?したがって、myProcess.StandardInput.Close()を試してください。

+0

私もそれを見ました。そして 'psi.RedirectStandardOutput = true;'はプログラムを終了します。出力は異常ですが終了します。 – Teletha

3

The MSDN provides a good exampleプロセスの入出力をどのようにリダイレクトできるか。 ReadToEnd()はストリームの終わりを正しく判別できません。 MSDN says

ReadToEndは、ストリームが終了に達した時点を知っているとみなします。あなたがそれを要求して接続を閉じることができないときにだけサーバーがデータを送信する対話型プロトコルの場合、ReadToEndは無期限にブロックされる可能性があるので、避けるべきです。

EDIT:ReadToEnd()を回避するために もう一つの理由:ストリームがプログラム出力する前に、すべてのデータをリダイレクトする必要があるため、非常に高速なプロセスは、例外が発生します。

0

は、代わりにそれが必要として、それの一部が終了していない

p.CloseMainWindow()

関連する問題