56 lines
2.1 KiB
C#
56 lines
2.1 KiB
C#
namespace TimerApp;
|
|
|
|
static class Program
|
|
{
|
|
/// <summary>
|
|
/// The main entry point for the application.
|
|
/// </summary>
|
|
[STAThread]
|
|
static void Main()
|
|
{
|
|
// 设置全局异常处理
|
|
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
|
|
Application.ThreadException += Application_ThreadException;
|
|
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
|
|
|
|
if (!SingleInstanceManager.TryAcquire(out var instance) || instance is null)
|
|
{
|
|
SingleInstanceManager.SignalExistingInstance();
|
|
return;
|
|
}
|
|
|
|
try
|
|
{
|
|
using (instance)
|
|
{
|
|
TaskbarIntegration.InitializeProcess();
|
|
ApplicationConfiguration.Initialize();
|
|
TaskbarIntegration.InitializeShortcuts();
|
|
|
|
var mainForm = new MainForm();
|
|
instance.StartServer(mainForm.ActivateFromExternal);
|
|
Application.Run(mainForm);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logger.LogError("Fatal error in Main", ex);
|
|
MessageBox.Show($"程序发生严重错误即将退出:\n{ex.Message}", "TimerApp Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
}
|
|
|
|
private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
|
|
{
|
|
Logger.LogError("Unhandled UI Exception", e.Exception);
|
|
// 这里可以选择不退出,或者提示用户
|
|
// MessageBox.Show("发生未知错误,程序将尝试继续运行。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
|
}
|
|
|
|
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
|
|
{
|
|
var ex = e.ExceptionObject as Exception;
|
|
Logger.LogError("Unhandled Domain Exception" + (e.IsTerminating ? " (Terminating)" : ""), ex);
|
|
}
|
|
|
|
}
|