if i am running a .net application and that application crashes frequently. i want to offer a crash recovery process such that after the crash, a message will be displayed to the user offering to restart the application. how is it possible to write the code that does this inside the application 开发者_StackOverflowand still run after it had crashed! sounds like a weird loop of code but i'd like to know your experience on this.
You can subscribe to AppDomain.CurrentDomain.UnhandledException
event and do the stuff from the event handler.
Write the code in the Main method. (Assuming it is Windows Application or Console Application it will have one).
Basically your main method ends up looking something like this:
public static Main(string[] args)
{
try
{
RunApplication(args);
}
catch(Exception ex)
{
RunCrashRecovery(args, ex);
}
}
Where RunApplication
is what you would have put in Main
originally and RunCrashRecovery
is the code that sends out the crash information and asks the user if they want to restart.
What do mean with crash? If you get an exception you could use try and catch to handle these errors.
If there is no exception you should try to change the code so that the program does not crash.
EDIT: Last is possible if you e.g. use the same variables in multiple threads.
精彩评论