view ServerMonitor/Program.cs @ 8:052aa62cb42a

Single instance. Add autorun option. Add icons. General enhancements.
author Brad Greco <brad@bgreco.net>
date Sat, 09 Mar 2019 20:14:03 -0500
parents 3e1a2131f897
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);
        }
    }
}