从 C# 调用 Fortran 可执行文件的过程

发布于 2025-01-06 21:02:37 字数 905 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

零度° 2025-01-13 21:02:37

当您启动的进程是带有消息循环的 Windows 应用程序时,使用 WaitForInputIdle()。就您而言,它是简单的控制台应用程序。

因此,您需要的只是从代码中排除两个 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();

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:

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();
奈何桥上唱咆哮 2025-01-13 21:02:37

只需删除两行 exeProcess.WaitForInputIdle(); 行,它们在您的情况下不是必需的。

Just remove the two exeProcess.WaitForInputIdle(); lines, they are not necessary in your case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文