3

I have a piece of C# code, that calls a process, pointing to another executable. In some rare occasions an access violation happens and the latter gets terminated by operating system with message "program.exe has stopped working ... windows can check online for a solution, blah-blah...". I am able to kill and close the process using WaitForExit with a predefined timeout, but the mentioned window keeps hanging. Is it possible to somehow dismiss it ?

The code, which calls external program is as follows :

ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardError = true;
startInfo.FileName = pathToExe;
startInfo.ErrorDialog = false;
startInfo.Arguments = arguments;
string error = "";


using (Process exeProcess = System.Diagnostics.Process.Start(startInfo))
{
    try
    {
        if (!exeProcess.WaitForExit(timeout))
        {
            /// timed out
            /// clean up, set err message
            exeProcess.Kill();
            error = "timed out";
        }
        else if (exeProcess.ExitCode != 0)
        {
            /// aborted itself on error
            /// clean up, return err message
            error = exeProcess.StandardError.ReadToEnd();
        }
        else
        {
           //finished correctly
           //do some useful stuff
        }
    }
    catch(Exception e)
    {
        error = e.Message;
    }
}
Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
user2082616
  • 195
  • 1
  • 17

1 Answers1

2

There's a Windows API call SetErrorMode which allows to disable this behavior. You can't however set the error mode for another process. This solution therefore implies that you had control over 'blah.exe' source code. Useful answers on the topic for C++ and C#.

Another suggested solution is to run your external program as a windows service.

Community
  • 1
  • 1
fan711
  • 716
  • 3
  • 13