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

Contents of /trunk/TSControlPanel/Program.cs

Parent Directory Parent Directory | Revision Log Revision Log


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

1 using System;
2 using System.Collections.Generic;
3 using System.Windows.Forms;
4 using System.Data.SqlClient;
5 using System.Collections;
6 using System.Text;
7
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 {
18 AppDomain currentDomain = AppDomain.CurrentDomain;
19 currentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
20 Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException, true);
21
22 Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
23
24 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
32 static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
33 {
34 CurrentDomain_UnhandledException(sender, new UnhandledExceptionEventArgs(e.Exception, false));
35 }
36 static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs args)
37 {
38 DialogResult result = DialogResult.Cancel;
39 Exception e = (Exception)args.ExceptionObject;
40 if (e == null)
41 {
42 Logging.TSControlPanelLog.Error("Encountered unhandled Exception, but the exception was null");
43 return;
44 }
45 if (e.GetType() == typeof(SqlException))
46 {
47 SqlException sql_ex = (e as SqlException);
48 Stack stack = new Stack();
49 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 stack = log4net.NDC.CloneStack();
52 Logging.TSControlPanelLog.Error("Encountered unhandled SqlException");
53 }
54 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 result = ShowAbortRetryIgnoreDialog("Encountered unhandled Exception", builder.ToString());
59 if (result == DialogResult.Abort)
60 Application.Exit();
61 }
62 else
63 {
64 Stack stack = new Stack();
65 using (log4net.NDC.Push(string.Format("Exception: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
66 {
67 stack = log4net.NDC.CloneStack();
68 Logging.TSControlPanelLog.Error("Encountered unhandled Exception");
69 }
70 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 result = ShowAbortRetryIgnoreDialog("Encountered unhandled Exception", builder.ToString());
75 if (result == DialogResult.Abort)
76 Application.Exit();
77 }
78 }
79 // 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 }
85 }

  ViewVC Help
Powered by ViewVC 1.1.22