comparison ServerMonitor/Program.cs @ 17:68d7834dc28e

More comments.
author Brad Greco <brad@bgreco.net>
date Sat, 25 May 2019 15:14:26 -0400
parents 052aa62cb42a
children 7645122aa7a9
comparison
equal deleted inserted replaced
16:7626b099aefd 17:68d7834dc28e
1 using System; 1 using System;
2 using System.Collections.Generic;
3 using System.Linq; 2 using System.Linq;
4 using System.Threading; 3 using System.Threading;
5 using System.Windows.Forms; 4 using System.Windows.Forms;
6 5
7 namespace ServerMonitorApp 6 namespace ServerMonitorApp
14 /// The main entry point for the application. 13 /// The main entry point for the application.
15 /// </summary> 14 /// </summary>
16 [STAThread] 15 [STAThread]
17 static void Main() 16 static void Main()
18 { 17 {
18 // Check if the program is already running.
19 if (mutex.WaitOne(TimeSpan.Zero, true)) 19 if (mutex.WaitOne(TimeSpan.Zero, true))
20 { 20 {
21 // Show unhandled exceptions to the user.
21 Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException); 22 Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
22 Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); 23 Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
23 AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); 24 AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
24 25
25 Application.EnableVisualStyles(); 26 Application.EnableVisualStyles();
26 Application.SetCompatibleTextRenderingDefault(false); 27 Application.SetCompatibleTextRenderingDefault(false);
27 Application.Run(new ServerSummaryForm()); 28 Application.Run(new ServerSummaryForm());
28 } 29 }
29 else 30 else
30 { 31 {
32 // If the program is already running, send a message to the main form
33 // asking it to show itself, then exit.
31 Win32Helpers.PostMessage( 34 Win32Helpers.PostMessage(
32 (IntPtr)Win32Helpers.HWND_BROADCAST, 35 (IntPtr)Win32Helpers.HWND_BROADCAST,
33 Win32Helpers.WM_SHOWMONITOR, 36 Win32Helpers.WM_SHOWMONITOR,
34 IntPtr.Zero, 37 IntPtr.Zero,
35 IntPtr.Zero); 38 IntPtr.Zero);
36 } 39 }
37 } 40 }
38 41
42 /// <summary>Shows unhandled exceptions in a message box.</summary>
39 static void Application_ThreadException(object sender, ThreadExceptionEventArgs e) 43 static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
40 { 44 {
41 //if (!System.Diagnostics.Debugger.IsAttached) 45 if (!System.Diagnostics.Debugger.IsAttached)
42 ShowError(e.Exception); 46 ShowError(e.Exception);
43 } 47 }
44 48
49 /// <summary>Shows unhandled exceptions in a message box.</summary>
45 static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) 50 static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
46 { 51 {
47 //if (!System.Diagnostics.Debugger.IsAttached) 52 if (!System.Diagnostics.Debugger.IsAttached)
48 ShowError(e.ExceptionObject as Exception); 53 ShowError(e.ExceptionObject as Exception);
49 } 54 }
50 55
56 /// <summary>Shows exception details in a message box.</summary>
57 /// <param name="e">The exception to show.</param>
51 static void ShowError(Exception e) 58 static void ShowError(Exception e)
52 { 59 {
53 string simpleStackTrace = string.Join(Environment.NewLine, new System.Diagnostics.StackTrace(e).GetFrames() 60 string simpleStackTrace = string.Join(Environment.NewLine, new System.Diagnostics.StackTrace(e).GetFrames()
54 .Select(f => f.GetMethod().DeclaringType.Name + "." + f.GetMethod().Name).ToArray()); 61 .Select(f => f.GetMethod().DeclaringType.Name + "." + f.GetMethod().Name).ToArray());
55 MessageBox.Show(e.Message + Environment.NewLine + Environment.NewLine + simpleStackTrace, "Server Monitor Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 62 MessageBox.Show(e.Message + Environment.NewLine + Environment.NewLine + simpleStackTrace, "Server Monitor Error", MessageBoxButtons.OK, MessageBoxIcon.Error);