2012-02-20 18 views
2

私はfortran実行可能ファイルを呼び出すプロセスを持っています。実行可能ファイルは、ユーザーからファイルを要求し、解決策を見つけるための操作を実行します。ファイル上に複数の解が見つかった場合、プログラムは、最も最適な解を探したいかどうかをユーザーに尋ねます。基本的にはプログラムの2つの入力です。実行可能ファイルは、プログラムの結果を提供するテキストファイルを生成します。C#入力を受け取っていないプロセス

プロセスは実行できますが、結果のテキストファイルは生成されません。また、アプリケーションの出力を確認したときに、メッセージプロンプト(「ファイルを入力」)が文字列に格納されている唯一のもので、最適なソリューションのセカンダリプロンプトが表示されません(「最も最適な解決策?」)。誰が私にこのことが起こっているのかというアイディアを教えてもらえますか?ありがとう。

Process exeProcess = new Process(); 
exeProcess.StartInfo.FileName = "sdf45.exe"; 
exeProcess.StartInfo.UseShellExecute = false; 
exeProcess.StartInfo.RedirectStandardError = true; 
exeProcess.StartInfo.RedirectStandardInput = true; 
exeProcess.StartInfo.RedirectStandardOutput = true; 
exeProcess.Start();   
//input file     
exeProcess.StandardInput.WriteLine(Path.GetFileName(filePath));    
//find optimal solution 
exeProcess.StandardInput.WriteLine("Y"); 
string output = exeProcess.StandardOutput.ReadToEnd();    
exeProcess.WaitForExit(); 
+0

実行ファイルはどのようにユーザーからファイルを要求しますか? – Tigran

+0

標準エラーを読まずにリダイレクトしています。それは、プログラムが標準エラーに多くを書き込む場合に問題を引き起こす可能性があります。 – Servy

+0

実行可能ファイルは、実行可能ファイルと同じディレクトリにあるファイルの名前を尋ねます。 – BeingIan

答えて

0

これは

+1

OPから、ファイル名がパラメータではなく(プロンプトの後に)stdinを介して渡されるようです。 –

+0

@ChrisShain:実際にはあまり明確ではない*その点。 Opがそれを明確にするならばいいだろう。 – Tigran

1

私の推測では、このラインで役に立てば幸いそれは言うのは難しいのですが、私はこの

Process exeProcess = new Process(); 
exeProcess.StartInfo.FileName = "sdf45.exe"; 
exeProcess.StartInfo.UseShellExecute = false; 
exeProcess.StartInfo.RedirectStandardError = true; 
exeProcess.StartInfo.RedirectStandardInput = true; 
exeProcess.StartInfo.RedirectStandardOutput = true; 
exeProcess.StartInfo.Arguments = Path.GetFileName(filePath); //pass file path to EXE 
exeProcess.Start(); 

のように、あなたが実行可能ファイルに引数を渡す必要がを推定FORTRANプロセスが入力を読み取る機会を得る前に実行(および戻す)しています:

string output = exeProcess.StandardOutput.ReadToEnd(); 

この場合、無制限ストリーム上のReadToEnd();の結果が何であるかを100%確信していません。ここに記載されているようにジョンスキートhereで述べたように、これを行うための適切な方法は、まだ非同期的により良い別のスレッドやで標準出力から読み取ることです。http://msdn.microsoft.com/en-us/library/system.diagnostics.process.beginoutputreadline.aspx

後世のための大まかな例:

var outputReader = new Thread(ReadOutput); 
outputReader.Start(exeProcess); 

あなたの最初のメソッドを作る

public void ReadOutput(Object processState) { 
    var process = processState as Process; 
    if (process == null) return; 
    var output = exeProcess.StandardOutput.ReadToEnd(); 
    // Do whetever with output 
} 

Process exeProcess = new Process(); 
exeProcess.StartInfo.FileName = "sdf45.exe"; 
exeProcess.StartInfo.UseShellExecute = false; 
exeProcess.StartInfo.RedirectStandardError = true; 
exeProcess.StartInfo.RedirectStandardInput = true; 
exeProcess.StartInfo.RedirectStandardOutput = true; 
exeProcess.Start();   
//input file     
exeProcess.StandardInput.WriteLine(Path.GetFileName(filePath));    
//find optimal solution 
exeProcess.StandardInput.WriteLine("Y"); 
var outputReader = new Thread(ReadOutput); 
outputReader.Start(exeProcess); 
exeProcess.WaitForExit(); 
outputReader.Join(); 
ReadOutputはこのような何かを定義している場合
+0

標準からの読み取りはまったく悪い考えではありませんが、私はそれがOPの問題であるとは思えません。ReadToEndはブロックされているので、プログラムがストリームメッセージの終わりを送信するまで戻りません。プログラムは終了準備が整うまで送信しません(平均/無能なプログラマがEOSトークンを標準出力に書き出しない限りより多くのコンテンツを書き出すことができます)。 – Servy

関連する問題