Hosam Aly
Hosam Aly

Reputation: 42443

Detecting and restarting my crashed .NET application

How can I detect that my .NET application has crashed, and then restart it?

Upvotes: 11

Views: 6357

Answers (4)

Hosam Aly
Hosam Aly

Reputation: 42443

Another solution (based on this example) is to create a launcher that controls the application:

class LauncherProgram
{
    static int count = 3;

    static void Main()
    {
        Launch();
        Thread.Sleep(Timeout.Infinite);
    }

    static void Launch()
    {
        Process process = new Process();
        process.StartInfo.FileName = "MyApp.exe";
        process.EnableRaisingEvents = true;
        process.Exited += LaunchIfCrashed;
        process.Start();
    }

    static void LaunchIfCrashed(object o, EventArgs e)
    {
        Process process = (Process) o;
        if (process.ExitCode != 0)
        {
            if (count-- > 0) // restart at max count times
                Launch();
            else
                Environment.Exit(process.ExitCode);
        }
        else
        {
            Environment.Exit(0);
        }
    }

Upvotes: 15

HTTP 410
HTTP 410

Reputation: 17608

If this is a Windows Forms app:

  • Set jitDebugging = true in App.Config. This prevents the built-in Windows Forms unhandled exception handler being triggered.

Now regardless of whether this is a Windows Forms app or a console app:

  • Register for the Application.ThreadException event, e.g. in C#:

    Application.ThreadException += new Threading.ThreadExceptionHandler(CatchFatalException);

At this point, your app is already on its way into a black hole. What happens next depends on whether or not this is a Windows Forms app:

  • If it's a Windows Forms app, call the Application.Restart method in your CatchFatalException event handler.
  • Otherwise you will instead need to p/invoke to the application restart and recovery native functions. That link discusses Vista, but in my tests it works just fine on XP as well.

Upvotes: 8

Marc Gravell
Marc Gravell

Reputation: 1062600

  • run the work inside an AppDomain; use the primary AppDomain to monitor it (doesn't guard against process kill, though)
  • lots of exception handling! i.e. don't let a fatal error tear down the process
  • run it in something that already has recycling built in - IIS for example

Upvotes: 5

Hosam Aly
Hosam Aly

Reputation: 42443

A possible solution is to create another process to monitor your application, and restart it if it is terminated:

class ProcessMonitorProgram
{
    const string myProcess = "MyApp.exe";

    static void Main()
    {
        new Timer(CheckProcess, null, 0, 60 * 1000);
        Thread.Sleep(Timeout.Infinite);
    }

    static void CheckProcess(object obj)
    {
        if (Process.GetProcessesByName(myProcess).Length == 0)
            Process.Start(myProcess);
    }
}

One of the problems with this solution is that it will keep the process restarting forever, until this monitoring application itself is terminated.

Upvotes: 1

Related Questions