2012-02-17 33 views
1

我有一個fortran可執行文件,我試圖用一個進程運行。 Fortran可執行文件向用戶請求文件,執行操作以查找解決方案,之後如果有多個解決方案,它會詢問用戶是否希望找到最佳解決方案。我提供的代碼在我調用等待輸入空閒時似乎總是崩潰,因爲似乎沒有gui。我不熟悉使用流程,所以如果有人可以幫助我,那將不勝感激。從C#調用Fortran可執行文件的進程

謝謝。

編輯:我忘了提及Fortran可執行文件在向用戶提供結果之後生成一個文本文件。當我刪除等待輸入空閒時,它不再崩潰,但生成的文本文件不再生成。可執行文件運行不正常?

 Process exeProcess = new Process(); 
     exeProcess.StartInfo.FileName = @"...\marcus12.exe"; 
     exeProcess.StartInfo.UseShellExecute = false; 
     exeProcess.StartInfo.RedirectStandardError = true; 
     exeProcess.StartInfo.RedirectStandardInput = true; 
     exeProcess.StartInfo.RedirectStandardOutput = true; 
     exeProcess.Start(); 
     //exeProcess.WaitForInputIdle(); 
     exeProcess.StandardInput.WriteLine(Path.GetFileName(filePath)); 
     //exeProcess.WaitForInputIdle(); 
     exeProcess.StandardInput.WriteLine("Y"); 
     exeProcess.WaitForExit(); 
+0

您不需要等待。 – SLaks 2012-02-17 19:26:05

+0

感謝您的迴應,它不再崩潰,但現在可執行文件沒有生成正確的輸出(請參閱編輯),這讓我質疑它是否正常運行。 – BeingIan 2012-02-17 19:43:10

+0

閱讀標準輸出和錯誤,看看會發生什麼 – SLaks 2012-02-17 20:41:27

回答

0

當您啓動的進程是一個帶有消息循環的Windows應用程序時,使用WaitForInputIdle()。在你的情況下它是簡單的控制檯應用程序

因此,所有你需要的是排除2 WaitForInputIdle()的調用的代碼:

Process exeProcess = new Process(); 
exeProcess.StartInfo.FileName = @"...\marcus12.exe"; 
exeProcess.StartInfo.UseShellExecute = false; 
exeProcess.StartInfo.RedirectStandardError = true; 
exeProcess.StartInfo.RedirectStandardInput = true; 
exeProcess.StartInfo.RedirectStandardOutput = true; 
exeProcess.Start(); 
exeProcess.StandardInput.WriteLine(Path.GetFileName(filePath)); 
exeProcess.StandardInput.WriteLine("Y"); 
exeProcess.WaitForExit(); 
0

只要刪除兩條exeProcess.WaitForInputIdle();行,它們在您的情況下不是必需的。

相關問題