What's the best way (standard solution maybe) to build crash recovery into my application so it can automatically restart on any kind of crash.
tnx.
What's the best way (standard solution maybe) to build crash recovery into my application so it can automatically restart on any kind of crash.
tnx.
You have a few options here.
The first (and best) one is to add some type of global error handling which will catch any otherwise uncaught exceptions and properly deal with them. Along these lines you should start adding the appropriate specific exception handling to your code base. Bear in mind that stack overflow and certain security and memory exceptions will blow past any global handling regardless.
A second option is to have a monitoring service which just tests to see if the current app is still working. If it isn't then force kill the existing app and restart a new instance.
A third option is to separate your application into two apps. An outer "container" type app that simply executes the other process. The container app won't have a UI, but will start the main process and watch it (much like option 2 above). I've seen this one used in various "modular" applications.
The point is, the only real way to do this is to have 2 apps: one to monitor, the other to actually do the UI and everything else.
It is in general better not to do this, there's nothing pretty about a process that constantly starts and immediately crashes again with the user helplessly looking at the carnage. But I can only hand you the bullets, aiming the gun at your foot is up to you. You'll need code like this:
static void Main(string[] args) {
AppDomain.CurrentDomain.UnhandledException += ReportAndRestart;
// etc..
}
static void ReportAndRestart(object sender, UnhandledExceptionEventArgs e) {
string info = e.ExceptionObject.ToString();
// Log or display info
//...
// Let the user know you're restarting
//...
// And restart:
System.Diagnostics.Process.Start(
System.Reflection.Assembly.GetEntryAssembly().Location,
string.Join(" ", Environment.GetCommandLineArgs()));
Environment.Exit(1);
}
}
Beware that I took a shortcut on the command line arguments. They should be quoted if they contain a path to a file that contains spaces. Don't take shortcuts on the code you're supposed to put in the ellipsis.
You have a few options here.
The first (and best) one is to add some type of global error handling which will catch any otherwise uncaught exceptions and properly deal with them. Along these lines you should start adding the appropriate specific exception handling to your code base. Bear in mind that stack overflow and certain security and memory exceptions will blow past any global handling regardless.
A second option is to have a monitoring service which just tests to see if the current app is still working. If it isn't then force kill the existing app and restart a new instance.
A third option is to separate your application into two apps. An outer "container" type app that simply executes the other process. The container app won't have a UI, but will start the main process and watch it (much like option 2 above). I've seen this one used in various "modular" applications.
The point is, the only real way to do this is to have 2 apps: one to monitor, the other to actually do the UI and everything else.
Put your persistent state in something that supports transactions. Eg. Database (sqlite) or if the needs are not too complex, use copy on write (write changes to a new file, and only if that was successful discard the old file)
These suggestions are very generic.
© 2022 - 2024 — McMap. All rights reserved.