1

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. 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.

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. I do realize that it will never return False, since the program gets hung-up before that point in the code. 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;
        }
GunnzATK
  • 13
  • 2

1 Answers1

0

you need to put your code in a try catch. you can also check this similar problem LINK and recommended solution.

Tobi Awe
  • 1
  • 3
  • Where exactly should the try go? I've tried a try-catch in the past, but it doesn't seem to help any. Also, I don't care what the error message is, since what the program spits out isn't useful at all, I just want to be able to display my own message that says "Process failed" or something. – GunnzATK May 05 '20 at 17:21