C Tips: Use custom abnormal handling to restart your app
When using the .NET structure, you will execute the TRY / CATCH block to process your application's special exception. One capability of .NET is that you can use custom anomalous to crawl your application untreated from the running process. This allows you to terminate your program and complete any program clearance and error record in a manner.
When a customer needs application records that are not processed and I often use this feature. I will explain how .NET how to complete this feature.
1. First, create a C # application (RestartApp)
2, add the following USING statement.
Using system.threading;
3. Add the following custom exception handling class, you can see only two methods:
• OnthreadException - Record an exception, tell the user that the application will start, turn off the current program, and then restart, in the next step you will see how this method will be automatically called as an exception handler.
• LOGEXCEPTION - This is a simple record method (called onthreadException) to write unprocessed exceptions to files with the file name in the current date.
1. // Custom Exception Class To Catch All "Unhandled Exceptions"
2. Public Class CustomExceptionHandler
3. {
4. // Event Handler That Will Be Called When An Unhandled
5. // Exception is caught
6. Public void onthreadException (Object Sender,
7. ThreadExceptionEventArgs T)
8. {
9. // Log the Exception to a file
10. LOGEXCEPTION (T.EXCEPTION);
11.
12. // Tell The User That The App Will Restart
13. MessageBox.show ("A Fatal Error Was Detected and Logged.
14. Click The Ok Button To Restart THE
15. Application ",
16. "Fatal Application Error",
17. MessageBoxButtons.ok, MessageBoxicon.Stop;
18.
19. // Shut Down The Current App Instance
20. Application.exit ();
twenty one.
22. // Restart the app
23. System.Diagnostics.Process.start (Application.executablePath);
twenty four. }
25.
26. // * Very * Simple Logging Function To Write Exception Details27. // To Disk
28. Protected void Logexception (Exception E)
29. {
30. DateTime Now = system.datetime.now;
31. String Error = E.Message "/ N / NSTACK TRACE: / N"
32. E.STACKTRACE;
33. String filename = string.format ("LOG- {0} {1} {2} - {3} {4}
34. {5} .txt ",
35. Now.year.toString (),
36. Now.month.toString (),
37. Now.day.tostring (),
38. Now. Hour, now.minute,
39. Now.second);
40.
41. Streamwriter stream = null;
42. TRY
43. {
44. Stream = New StreamWriter (FileName, False);
45. Stream.write (Error);
46.}
47. Catch (Exception EX)
48. {
49. MessageBox.show (ex.Message);
50.}
51. Finally
52. {
53. IF (NULL! = Stream)
54. stream.close ();
55.}
56.}
57.}
When the custom exception handling class is added, by adding the following two lines of code to the main function (in front of Application.Run), it is integrated into the structure, when the second line is attached to the CustomExceptionHandler.OnthreadException to the TREADEXCEPTION event, the first line will Instantiate the previously defined classes, and OntReadexception will be automatically called after an exception is abandoned.
58. CustomExceptionHandler EH = New CustomExceptionHandler ();
59. Application.threadException = new system.threading.