#nullable enable using System; using System.IO; using System.Threading.Tasks; using System.Windows; using System.Windows.Threading; using Microsoft.Extensions.DependencyInjection; using ReminderOrganizer.Services; using ReminderOrganizer.ViewModels; namespace ReminderOrganizer { public partial class App : Application { protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); // Only one instance: a second start would show duplicate reminders and race on tasks.json. // Test runs with their own data folder (REMINDER_ORGANIZER_DATA) get their own lock. var mutexName = "MichaelMedia.ReminderOrganizer" + (Environment.GetEnvironmentVariable("REMINDER_ORGANIZER_DATA") is { Length: > 0 } d ? "." + d.GetHashCode() : ""); _singleInstance = new System.Threading.Mutex(true, mutexName, out var isFirst); if (!isFirst) { Shutdown(); return; } SetupExceptionHandling(); ServiceLocator.Initialize(); // Buttons in Windows notifications (snooze / done / open) – also when Windows started us for it. Microsoft.Toolkit.Uwp.Notifications.ToastNotificationManagerCompat.OnActivated += args => { var toastArgs = Microsoft.Toolkit.Uwp.Notifications.ToastArguments.Parse(args.Argument); Current.Dispatcher.BeginInvoke(new Action(() => (Current.MainWindow as MainWindow)?.HandleToast(toastArgs))); }; bool fromToast = Microsoft.Toolkit.Uwp.Notifications.ToastNotificationManagerCompat.WasCurrentProcessToastActivated(); var main = new MainWindow(); MainWindow = main; // Autostart passes --minimized: start straight into the tray. A toast button decides itself. if (Array.IndexOf(e.Args, "--minimized") >= 0 || fromToast) main.StartHidden(); else main.Show(); } private System.Threading.Mutex? _singleInstance; /// Really exit (tray menu / "Exit"): the main window otherwise only hides to the tray. public static void Quit() { (Current.MainWindow as MainWindow)?.AllowClose(); Current.Shutdown(); } protected override void OnExit(ExitEventArgs e) { (MainWindow as MainWindow)?.OnAppExit(); Services.NotificationService.SharedTrayIcon?.Dispose(); _singleInstance?.Dispose(); base.OnExit(e); } private void SetupExceptionHandling() { AppDomain.CurrentDomain.UnhandledException += (s, e) => { LogException((Exception)e.ExceptionObject, "AppDomain.UnhandledException"); if (e.IsTerminating) Environment.Exit(1); }; DispatcherUnhandledException += (s, e) => { LogException(e.Exception, "DispatcherUnhandledException"); e.Handled = true; MessageBox.Show($"An unexpected error occurred:\n{e.Exception.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error); }; TaskScheduler.UnobservedTaskException += (s, e) => { LogException(e.Exception, "UnobservedTaskException"); e.SetObserved(); }; } private static void LogException(Exception ex, string source) { try { Directory.CreateDirectory(DataService.AppDirectory); var logPath = Path.Combine(DataService.AppDirectory, "error.log"); var logEntry = $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] [{source}]\n{ex}\n\n"; File.AppendAllText(logPath, logEntry); } catch { } } } }