/[AnywhereTS-MSSQL]/trunk/TSControlPanel/Program.cs
ViewVC logotype

Annotation of /trunk/TSControlPanel/Program.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 68 - (hide annotations) (download)
Thu Jul 12 19:40:36 2012 UTC (9 years, 2 months ago) by william
File size: 4121 byte(s)
+ show abort, retry, ignore dialog to user when unhandled exception is shown

1 william 4 using System;
2     using System.Collections.Generic;
3     using System.Windows.Forms;
4 william 52 using System.Data.SqlClient;
5 william 66 using System.Collections;
6     using System.Text;
7 william 4
8     namespace AnywhereTS
9     {
10     static class Program
11     {
12     /// <summary>
13     /// The main entry point for the application.
14     /// </summary>
15     [STAThread]
16     static void Main()
17 william 61 {
18     AppDomain currentDomain = AppDomain.CurrentDomain;
19     currentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
20     Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException, true);
21    
22 william 65 Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
23    
24 william 4 Application.EnableVisualStyles();
25     Application.SetCompatibleTextRenderingDefault(false);
26    
27     frmClientProperties objCustomDialogBox = new frmClientProperties();
28     objCustomDialogBox.dialogMode = frmClientProperties.ATSClientMode.CONTROL_PANEL; // Select the mode to run the form in.
29     Application.Run(objCustomDialogBox);
30     }
31 william 65
32     static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
33     {
34     CurrentDomain_UnhandledException(sender, new UnhandledExceptionEventArgs(e.Exception, false));
35     }
36 william 61 static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs args)
37     {
38 william 68 DialogResult result = DialogResult.Cancel;
39 william 61 Exception e = (Exception)args.ExceptionObject;
40     if (e == null)
41     {
42 william 66 Logging.TSControlPanelLog.Error("Encountered unhandled Exception, but the exception was null");
43 william 61 return;
44     }
45     if (e.GetType() == typeof(SqlException))
46     {
47     SqlException sql_ex = (e as SqlException);
48 william 67 Stack stack = new Stack();
49 william 61 using (log4net.NDC.Push(string.Format("SqlException: ID={0} MESSAGE={1}{2}Diagnostics:{2}{3}", sql_ex.Number.ToString(), sql_ex.Message, System.Environment.NewLine, sql_ex.ToString())))
50     {
51 william 67 stack = log4net.NDC.CloneStack();
52 william 66 Logging.TSControlPanelLog.Error("Encountered unhandled SqlException");
53 william 61 }
54 william 66 List<object> strs = new List<object>(stack.ToArray());
55     strs.Reverse();
56     StringBuilder builder = new StringBuilder();
57     foreach (object str in strs) { builder.AppendLine(str.ToString()); }
58 william 68 result = ShowAbortRetryIgnoreDialog("Encountered unhandled Exception", builder.ToString());
59     if (result == DialogResult.Abort)
60     Application.Exit();
61 william 61 }
62     else
63     {
64 william 67 Stack stack = new Stack();
65 william 61 using (log4net.NDC.Push(string.Format("Exception: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
66     {
67 william 67 stack = log4net.NDC.CloneStack();
68 william 66 Logging.TSControlPanelLog.Error("Encountered unhandled Exception");
69 william 67 }
70 william 66 List<object> strs = new List<object>(stack.ToArray());
71     strs.Reverse();
72     StringBuilder builder = new StringBuilder();
73     foreach (object str in strs) { builder.AppendLine(str.ToString()); }
74 william 68 result = ShowAbortRetryIgnoreDialog("Encountered unhandled Exception", builder.ToString());
75     if (result == DialogResult.Abort)
76     Application.Exit();
77 william 61 }
78     }
79 william 68 // Creates the error message and displays it.
80     private static DialogResult ShowAbortRetryIgnoreDialog(string title, string message)
81     {
82     return MessageBox.Show(message, title, MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop, MessageBoxDefaultButton.Button1);
83     }
84 william 4 }
85     }

  ViewVC Help
Powered by ViewVC 1.1.22