【发布时间】:2020-05-05 16:14:12
【问题描述】:
我有一个接口,它为另一个程序生成输入文件以计算泵性能,然后运行该程序并收集输出。如果输入的第三方程序中的计算不满意,第三方程序将崩溃并给出神秘的错误消息,无论你点击多少次“确定”都会继续弹出。第三方程序只是我拥有的一个 .exe 文件,我自己无权访问源代码。
有没有办法调用这个程序,但如果它崩溃,让我的界面杀死它?目前,我唯一的解决方法是打开我的任务管理器并手动终止该进程。
下面是我调用程序运行的代码部分。 “cpump.exe”是我指的第三方程序。我确实意识到它永远不会返回 False,因为程序在代码中的那个点之前就挂断了。请原谅我的无知/拙劣的作风,我绝不是程序员/开发人员,只是一个试图自动化他一些无聊工作的机械工程!
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = "c:\\cpump\\file\\cpump.exe";
start.WindowStyle = ProcessWindowStyle.Hidden;
start.UseShellExecute = false;
start.WorkingDirectory = "c:\\cpump\\file\\";
start.Arguments = "/k";
start.CreateNoWindow = true;
int exitCode;
// Run the external process & wait for it to finish
using (Process proc = Process.Start(start))
{
proc.WaitForExit();
// Retrieve the app's exit code
exitCode = proc.ExitCode;
}
if (exitCode == 0)
{
MessageBox.Show("Sucessful calculation complete.");
return true;
}
else
{
return false;
}
【问题讨论】: