从 C# 调用 Fortran 可执行文件的过程
我有一个 Fortran 可执行文件,我试图用一个进程来运行它。 Fortran 可执行文件向用户请求文件,执行操作以查找解决方案,然后如果有多个解决方案,它将询问用户是否想要找到最佳解决方案。当我调用等待输入空闲时,我提供的代码似乎总是崩溃,因为似乎没有 gui。我是使用流程的新手,因此如果有人可以帮助我,我将不胜感激。
谢谢。
编辑:我忘了提及 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();
I have a fortran executable that I'm trying to run with a process. The fortran executable requests a file from the user, performs an operation to find a solution, and afterwards if there are multiple solutions, it will ask the user if the would like to find the most optimal solution. My code here that I have provided seems to always crash when I call wait for input idle, as there doesn't seem to be a gui. I'm new to using processes so if someone could help me out, that would be appreciated.
Thanks.
Edit: I forgot to mention that the fortran executable generates a text file after to provide results to the user. When I remove the wait for input idle, it no longer crashes but the resulting text file is no longer generated. Is the executable not running properly?
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();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您启动的进程是带有消息循环的 Windows 应用程序时,使用 WaitForInputIdle()。就您而言,它是简单的控制台应用程序。
因此,您需要的只是从代码中排除两个 WaitForInputIdle() 调用:
WaitForInputIdle() is used when the process you launch is a Windows application with message loop. In your case it's simple console application.
Therefore all you need is to exclude two WaitForInputIdle() calls from the code:
只需删除两行 exeProcess.WaitForInputIdle(); 行,它们在您的情况下不是必需的。
Just remove the two
exeProcess.WaitForInputIdle();
lines, they are not necessary in your case.