10

In my console application I have code which looks like

    Process DKU = new Process();
    DKU.StartInfo.FileName = "MSSQLExecutor.exe";
    DKU.Start();
    DKU.WaitForExit();
    Console.WriteLine("lets move on ");

This is working fine and it waits until MSSQLExecutor.exe finishes its job, then after that the app continues.

My problem is that sometimes MSSQLExecutor.exe crashes and Windows by default shows a dialog for ending the program. At that point my application will wait forever for the user to click the Close button.

I want to avoid this because MY application is going to run as a service without user interaction.

After This I wanna to move on whit my app

squillman
  • 13,363
  • 3
  • 41
  • 60
adopilot
  • 4,340
  • 12
  • 65
  • 92

4 Answers4

8

The Dialog prevents that the process exists.

  1. If MSSQLExecutor is your Application you should fix the problem. But you can reach the goal (hiding the Dialog). Handle Application.ThreadException and AppDomain.CurrentDomain.UnhandledException

    Application.ThreadException +=new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
    AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
    
    
    static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
    {
        // log the exception
    }
    
    static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
    {
        // log the exception
    }
    
  2. If MSSQLExecutor is not your Application you can deactivate Dr. Watson

    Dr. Watson is an application debugger included with the Microsoft Windows operating system.

    • Click Start, click Run, type regedit.exe in the Open box, and then click OK.
    • Locate and then click the following registry key:
      HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\AeDebug
    • Click the AeDebug key, and then click Export Registry File on the Registry menu.
    • Delete the AeDebug key.

More Infromation

dknaack
  • 60,192
  • 27
  • 155
  • 202
  • Interesting. And if Dr. Watson isn't enabled, the process will die, as usual? And does that mean Process.WaitForExit() will stop blocking? – cwharris Feb 06 '12 at 15:15
  • fyi I'm investigating if this'll solve a problem I'm encountering; it certainly looks like it. Additional info about Dr. Watson, including what looks like a safer way of disabling it than deleting the registry key: http://techdows.com/2009/03/how-to-fix-drwatson-postmortem-debugger.html http://ccm.net/faq/6610-how-to-delete-disable-dr-watson-debugger – jhocking Jan 15 '18 at 17:58
  • @dknaack should I put this code in `Program.cs` or each of my forms do it doesn't crash on clients' systems ? – mrid Sep 04 '18 at 06:28
6

This is a frequent problem running outside processes. From here I would suggest setting a maximum timeout value for this application.

Process DKU = new Process();
DKU.StartInfo.FileName = "MSSQLExecutor.exe";
DKU.Start();
DKU.WaitForExit(10*60*1000);

if (!DKU.HasExited)
        DKU.Kill();

Console.WriteLine("lets move on ");
Community
  • 1
  • 1
DanTheMan
  • 3,277
  • 2
  • 21
  • 40
1

Relevent: Detecting process crash in .NET

Also, you could call Process.Kill if you only wanted to wait so long for the process to finish.

Community
  • 1
  • 1
cwharris
  • 17,835
  • 4
  • 44
  • 64
0

If you don't want to block waiting for the process to end, don't call Process.WaitForExit(). What's the goal of doing that? If you just need to know when it finishes, register for the Process.Exited event. In the Process.Exited event, you can check the Process.ExitCode to know whether it ended because of a crash or not.

MNGwinn
  • 2,394
  • 17
  • 18