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");
///
/// The main entry point for the application.
///
[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);
}
}
/// Shows unhandled exceptions in a message box.
static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
{
if (!System.Diagnostics.Debugger.IsAttached)
ShowError(e.Exception);
}
/// Shows unhandled exceptions in a message box.
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
if (!System.Diagnostics.Debugger.IsAttached)
ShowError(e.ExceptionObject as Exception);
}
/// Shows exception details in a message box.
/// The exception to show.
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);
}
}
}