2012-02-20 48 views
2

我已经调用一个FORTRAN可执行的处理。可执行文件向用户请求一个文件并执行操作以查找解决方案。如果在文件中找到多个解决方案,程序会询问用户是否想要找到最佳解决方案,基本上该程序有两个输入。然后可执行文件生成一个提供程序结果的文本文件。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 2012-02-20 20:50:28

+0

所以我看到你在不阅读它的情况下重定向标准错误。如果程序写很多标准错误,这可能会导致问题。 – Servy 2012-02-20 20:52:03

+0

可执行文件要求提供与可执行文件位于同一目录中的文件的名称。 – BeingIan 2012-02-20 20:52:57

回答

0

这很难说,但我假定,你需要一个参数传递到可执行文件,这样

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(); 

希望这有助于

+1

从OP看来,文件名似乎是通过stdin(在提示后)传递的,而不是参数。 – 2012-02-20 20:54:03

+0

@ChrisShain:实际上并不十分清楚*那*点。如果欧普澄清它会好起来的。 – Tigran 2012-02-20 21:13:45

1

我的猜测是,这条线是FORTRAN过程甚至有机会来读取输入之前执行(和返回):

string output = exeProcess.StandardOutput.ReadToEnd(); 

我不是100%肯定什么ReadToEnd();无界流的结果是在这种情况下。做正确的方法,如通过乔恩斯基特here提到的,是从标准输出在另​​一个线程读取或更好的异步,如记录在这里:http://msdn.microsoft.com/en-us/library/system.diagnostics.process.beginoutputreadline.aspx

为后人的缘故,一个粗略的例子:

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

其中ReadOutput的定义是这样的:

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(); 
+0

虽然从标准读出并不是一个坏主意,但我认为这不太可能是OP的问题。ReadToEnd是阻塞的,所以它不会返回,直到程序发送它是流结束消息,在程序准备退出之前程序不会发送消息(除非某些平均/无能的程序员在仍然存在时将EOS标记写入标准输出更多的内容写出来)。 – Servy 2012-02-20 22:28:44