view ServerMonitor/Program.cs @ 11:75ca86e0862c

Add setting to hide to notification area.
author Brad Greco <brad@bgreco.net>
date Mon, 15 Apr 2019 19:24:25 -0400
parents 052aa62cb42a
children 68d7834dc28e
line wrap: on
line source

using System;
using System.Collections.Generic;
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()
        {
            if (mutex.WaitOne(TimeSpan.Zero, true))
            {
                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
            {
                Win32Helpers.PostMessage(
                    (IntPtr)Win32Helpers.HWND_BROADCAST,
                    Win32Helpers.WM_SHOWMONITOR,
                    IntPtr.Zero,
                    IntPtr.Zero);
            }
        }

        static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
        {
            //if (!System.Diagnostics.Debugger.IsAttached)
                ShowError(e.Exception);
        }

        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            //if (!System.Diagnostics.Debugger.IsAttached)
                ShowError(e.ExceptionObject as Exception);
        }

        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);
        }
    }
}