简体   繁体   English

防止第三方应用程序崩溃阻止程序?

[英]Prevent third-party app crash from holding up program?

I have an interface that generates an input file for another program to calculate pump performance and then runs that program and collects the output.我有一个接口,它为另一个程序生成输入文件以计算泵性能,然后运行该程序并收集 output。 If the input is not satisfactory for the calculations in the third-party program, the third-party program will crash and give cryptic error-messages that will continue to pop up no matter how many times you hit "OK".如果输入的第三方程序中的计算不满意,第三方程序将崩溃并给出神秘的错误消息,无论你点击多少次“确定”都会继续弹出。 The third-party program is just a.exe file that I have, and I do not have access to the source-code myself.第三方程序只是我拥有的一个.exe文件,我自己无权访问源代码。

Is there a way to call this program, but allow my interface to kill it if it crashes?有没有办法调用这个程序,但如果它崩溃,让我的界面杀死它? Currently, my only work around is to open my Task Manager and manually kill the process.目前,我唯一的解决方法是打开我的任务管理器并手动终止该进程。

Below is my section of code that calls the program to run.下面是我调用程序运行的代码部分。 "cpump.exe" is the third-party program I am referring to. “cpump.exe”是我指的第三方程序。 I do realize that it will never return False, since the program gets hung-up before that point in the code.我确实意识到它永远不会返回 False,因为程序在代码中的那个点之前就挂断了。 Please excuse my ignorance/poor style, I'm by no means a programmer/developer, just a mechanical engineering trying to automate some of his boring work!请原谅我的无知/糟糕的作风,我绝不是程序员/开发人员,只是一个试图自动化他一些无聊工作的机械工程!

        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;
        }

you need to put your code in a try catch.你需要把你的代码放在一个try catch中。 you can also check this similar problem LINK and recommended solution.你也可以查看这个类似的问题LINK和推荐的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM