view ServerMonitor/Program.cs @ 22:48044d9ac000

Change default check interval from 5 seconds to 5 minutes.
author Brad Greco <brad@bgreco.net>
date Thu, 30 May 2019 21:40:02 -0400
parents 68d7834dc28e
children 7645122aa7a9
line wrap: on
line source

using System;
using System.Linq;
using System.Threading;
using System.Windows.Forms;

namespace ServerMonitorApp
{
    static class Program
    {
        static Mutex mutex = new Mutex(true, "c681570e-dff4-45fa-bdca-d8ca928a0f8a");

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            // Check if the program is already running.
            if (mutex.WaitOne(TimeSpan.Zero, true))
            {
                // Show unhandled exceptions to the user.
                Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
                Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
                AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new ServerSummaryForm());
            }
            else
            {
                // If the program is already running, send a message to the main form
                // asking it to show itself, then exit.
                Win32Helpers.PostMessage(
                    (IntPtr)Win32Helpers.HWND_BROADCAST,
                    Win32Helpers.WM_SHOWMONITOR,
                    IntPtr.Zero,
                    IntPtr.Zero);
            }
        }

        /// <summary>Shows unhandled exceptions in a message box.</summary>
        static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
        {
            if (!System.Diagnostics.Debugger.IsAttached)
                ShowError(e.Exception);
        }

        /// <summary>Shows unhandled exceptions in a message box.</summary>
        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            if (!System.Diagnostics.Debugger.IsAttached)
                ShowError(e.ExceptionObject as Exception);
        }

        /// <summary>Shows exception details in a message box.</summary>
        /// <param name="e">The exception to show.</param>
        static void ShowError(Exception e)
        {
            string simpleStackTrace = string.Join(Environment.NewLine, new System.Diagnostics.StackTrace(e).GetFrames()
                    .Select(f => f.GetMethod().DeclaringType.Name + "." + f.GetMethod().Name).ToArray());
            MessageBox.Show(e.Message + Environment.NewLine + Environment.NewLine + simpleStackTrace, "Server Monitor Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
}