繁体   English   中英

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

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

我有一个接口,它为另一个程序生成输入文件以计算泵性能,然后运行该程序并收集 output。 如果输入的第三方程序中的计算不满意,第三方程序将崩溃并给出神秘的错误消息,无论你点击多少次“确定”都会继续弹出。 第三方程序只是我拥有的一个.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;
        }

你需要把你的代码放在一个try catch中。 你也可以查看这个类似的问题LINK和推荐的解决方案。

暂无
暂无

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

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