#if DEBUG using ThreadPilot.Tests; #endif using ThreadPilot.Services; using ThreadPilot.ViewModels; namespace ThreadPilot { using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; using System.Windows; using System.Windows.Threading; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using ThreadPilot.Helpers; using ThreadPilot.Models; public partial class App : System.Windows.Application { private const string RegisterLaunchTaskArgument = "--register-launch-task"; private const string LaunchedViaTaskArgument = "--launched-via-task"; private Mutex? singleInstanceMutex; private int uiExceptionDialogOpen; private DateTime lastUiExceptionDialogUtc = DateTime.MinValue; public IServiceProvider ServiceProvider { get; private set; } public App() { ServiceCollection services = new ServiceCollection(); // Use the new centralized service configuration services.ConfigureApplicationServices(); this.ServiceProvider = services.BuildServiceProvider(); // Validate service configuration ServiceConfiguration.ValidateServiceConfiguration(this.ServiceProvider); } protected override void OnStartup(StartupEventArgs e) { // Parse command line arguments early so special startup modes can short-circuit normal flow. var startupMode = StartupMode.Parse(e.Args); bool effectiveStartMinimized = false; ApplicationSettingsModel? loadedSettings = null; effectiveStartMinimized = startupMode.StartMinimized; if (startupMode.IsSmokeTest) { var smokeLogger = this.ServiceProvider.GetRequiredService>(); var smokeTestResult = this.RunSmokeTestWithTimeout(smokeLogger, TimeSpan.FromSeconds(10)); Environment.ExitCode = smokeTestResult; this.Shutdown(smokeTestResult); Environment.Exit(smokeTestResult); return; } // Set up global exception handlers first AppDomain.CurrentDomain.UnhandledException += this.OnUnhandledException; this.DispatcherUnhandledException += this.OnDispatcherUnhandledException; TaskScheduler.UnobservedTaskException += this.OnUnobservedTaskException; // Check elevation status first var elevationService = this.ServiceProvider.GetRequiredService(); var elevatedTaskService = this.ServiceProvider.GetRequiredService(); var logger = this.ServiceProvider.GetRequiredService>(); var isRunningAsAdministrator = elevationService.IsRunningAsAdministrator(); if (isRunningAsAdministrator) { logger.LogInformation("Application is running with administrator privileges"); } else { if (startupMode.LaunchedViaTask) { logger.LogError("Application was launched via managed task marker but is still not elevated."); } #if DEBUG else if (!startupMode.IsTestMode) #else else #endif { var launchedElevatedInstance = Task.Run(async () => await elevatedTaskService.TryRunLaunchTaskAsync()).GetAwaiter().GetResult(); if (launchedElevatedInstance) { logger.LogInformation("Managed elevated launch task started successfully. Exiting current non-elevated instance."); this.Shutdown(); return; } if (!startupMode.RegisterLaunchTask) { logger.LogInformation("Managed elevated launch task is unavailable. Requesting one-time elevation to bootstrap persistent launch."); var restartInitiated = Task.Run(async () => await elevationService.RestartWithElevation(new[] { RegisterLaunchTaskArgument })).GetAwaiter().GetResult(); if (restartInitiated) { return; } } } #if DEBUG if (!startupMode.IsTestMode) #else if (true) #endif { logger.LogError("ThreadPilot requires administrator privileges and cannot continue without elevation."); this.ShowElevationRequiredMessage(); this.Shutdown(1); return; } } // Enforce single-instance after elevation bootstrap logic to avoid mutex races during handoff. bool createdNew; try { this.singleInstanceMutex = new Mutex(initiallyOwned: true, name: "Global\\ThreadPilot_SingleInstance", createdNew: out createdNew); } catch (UnauthorizedAccessException) { // Created by another user's session with an ACL we cannot open: an instance is already running there. createdNew = false; } if (!createdNew) { System.Windows.MessageBox.Show( "ThreadPilot is already running.", "Instance already open", MessageBoxButton.OK, MessageBoxImage.Information); this.Shutdown(); return; } base.OnStartup(e); // Check for test mode #if DEBUG if (startupMode.IsTestMode) { // Run in console test mode AllocConsole(); _ = Task.Run(async () => { await TestRunner.RunTests(); this.Dispatcher.Invoke(() => this.Shutdown()); }); return; } #endif try { var settingsService = this.ServiceProvider.GetRequiredService(); var themeService = this.ServiceProvider.GetRequiredService(); var localizationService = this.ServiceProvider.GetRequiredService(); Task.Run(async () => await settingsService.LoadSettingsAsync()).GetAwaiter().GetResult(); var settings = settingsService.Settings; loadedSettings = settings; localizationService.ApplyLanguage(settings.Language); effectiveStartMinimized = startupMode.StartMinimized || settings.StartMinimized; var useDarkTheme = settings.HasUserThemePreference ? settings.UseDarkTheme : themeService.GetSystemUsesDarkTheme(); if (!settings.HasUserThemePreference && settings.UseDarkTheme != useDarkTheme) { settings.UseDarkTheme = useDarkTheme; Task.Run(async () => await settingsService.UpdateSettingsAsync(settings)).GetAwaiter().GetResult(); } themeService.ApplyTheme(useDarkTheme); } catch (Exception ex) { logger.LogWarning(ex, "Failed to preload theme settings during startup"); } if (isRunningAsAdministrator) { // Off the UI thread and in this order: the launch-task pass deletes tasks pointing outside Program // Files, then autostart is restored for this copy if the user asked for it (e.g. right after a // portable copy installed itself in Program Files). var autostartService = this.ServiceProvider.GetRequiredService(); var wantsAutostart = loadedSettings?.AutostartWithWindows == true; var autostartMinimized = loadedSettings?.StartMinimized ?? true; TaskSafety.FireAndForget( Task.Run(async () => { if (!await elevatedTaskService.EnsureLaunchTaskAsync()) { logger.LogInformation("Elevated launch task not available; the next launch will ask for elevation."); } if (wantsAutostart) { await autostartService.EnsureAutostartAsync(autostartMinimized); } new UpdateTempDirectoryProvider().CleanupStale(TimeSpan.FromDays(1)); }), ex => logger.LogWarning(ex, "Failed to ensure elevated tasks during startup")); } var mainWindow = this.ServiceProvider.GetRequiredService(); this.MainWindow = mainWindow; // Handle startup behavior with comprehensive error handling try { logger.LogInformation("Attempting to show main window..."); // Ensure the window is properly initialized if (mainWindow == null) { throw new InvalidOperationException("MainWindow could not be created"); } var startupWindowBehavior = StartupWindowBehavior.Resolve(startupMode.IsAutostart, effectiveStartMinimized, UiStateStore.Current.WindowMaximized); var showStartupSuggestion = loadedSettings != null && StartupMinimizedSuggestionPolicy.ShouldShow(loadedSettings, startupWindowBehavior); mainWindow.ConfigureStartupMode( isSilentStartupMode: !startupWindowBehavior.ShouldShowWindow, showStartupMinimizedSuggestionOnReady: showStartupSuggestion); mainWindow.ShowInTaskbar = startupWindowBehavior.ShowInTaskbar; mainWindow.Visibility = startupWindowBehavior.Visibility; mainWindow.WindowState = startupWindowBehavior.WindowState; if (startupWindowBehavior.ShouldShowWindow) { mainWindow.Show(); if (startupWindowBehavior.HideAfterShow) { mainWindow.Hide(); } else if (startupWindowBehavior.ActivateAfterShow) { mainWindow.EnsureDashboardVisibleOnScreen(); mainWindow.Activate(); } } logger.LogInformation("Startup window behavior applied successfully"); } catch (Exception ex) { logger.LogError(ex, "Critical error during application startup"); // Show error message and exit gracefully var errorMessage = $"ThreadPilot failed to start:\n\n{ex.Message}\n\nStack Trace:\n{ex.StackTrace}"; System.Windows.MessageBox.Show(errorMessage, "ThreadPilot Startup Error", MessageBoxButton.OK, MessageBoxImage.Error); // Exit the application this.Shutdown(1); return; } } private int RunSmokeTestWithTimeout(ILogger logger, TimeSpan timeout) { var smokeTestTask = Task.Run(() => this.RunSmokeTest(logger)); if (smokeTestTask.Wait(timeout)) { return smokeTestTask.GetAwaiter().GetResult(); } logger.LogError("ThreadPilot smoke test timed out after {TimeoutSeconds} seconds", timeout.TotalSeconds); return 2; } private int RunSmokeTest(ILogger logger) { try { logger.LogInformation("Starting ThreadPilot smoke test"); _ = this.ServiceProvider.GetRequiredService(); _ = this.ServiceProvider.GetRequiredService(); _ = this.ServiceProvider.GetRequiredService(); _ = this.ServiceProvider.GetRequiredService(); if (!System.IO.Directory.Exists(AppContext.BaseDirectory)) { throw new InvalidOperationException("Application base directory was not found."); } logger.LogInformation("ThreadPilot smoke test completed successfully"); return 0; } catch (Exception ex) { logger.LogError(ex, "ThreadPilot smoke test failed"); return 1; } } private readonly struct StartupMode { public bool StartMinimized { get; init; } public bool IsAutostart { get; init; } public bool IsSmokeTest { get; init; } public bool RegisterLaunchTask { get; init; } public bool LaunchedViaTask { get; init; } public bool IsTestMode { get; init; } public static StartupMode Parse(IEnumerable args) { var mode = default(StartupMode); foreach (var arg in args) { switch (arg.ToLowerInvariant()) { case "--test": mode = mode with { IsTestMode = true }; break; case "--smoke-test": mode = mode with { IsSmokeTest = true }; break; case "--start-minimized": mode = mode with { StartMinimized = true }; break; case "--autostart": mode = mode with { IsAutostart = true }; break; case "--startup": mode = mode with { IsAutostart = true, StartMinimized = true }; break; case RegisterLaunchTaskArgument: mode = mode with { RegisterLaunchTask = true }; break; case LaunchedViaTaskArgument: mode = mode with { LaunchedViaTask = true }; break; } } return mode; } } protected override void OnExit(ExitEventArgs e) { AppDomain.CurrentDomain.UnhandledException -= this.OnUnhandledException; this.DispatcherUnhandledException -= this.OnDispatcherUnhandledException; TaskScheduler.UnobservedTaskException -= this.OnUnobservedTaskException; this.DisposeServiceProvider(); if (this.singleInstanceMutex != null) { try { this.singleInstanceMutex.ReleaseMutex(); } catch { // Ignore; we just want to clean up quietly } this.singleInstanceMutex.Dispose(); this.singleInstanceMutex = null; } base.OnExit(e); } private void DisposeServiceProvider() { if (this.ServiceProvider is not IDisposable disposableProvider) { return; } var logger = this.ServiceProvider.GetService>(); try { disposableProvider.Dispose(); } catch (Exception ex) { logger?.LogWarning(ex, "Failed to dispose the application service provider during shutdown"); } } #if DEBUG [System.Runtime.InteropServices.DllImport("kernel32.dll")] private static extern bool AllocConsole(); #endif private void ShowElevationRequiredMessage() { // Don't show the message during autostart to avoid interrupting the user var args = Environment.GetCommandLineArgs(); if (args.Any(arg => arg.Equals("--autostart", StringComparison.OrdinalIgnoreCase) || arg.Equals("--startup", StringComparison.OrdinalIgnoreCase))) { return; } System.Windows.MessageBox.Show( "ThreadPilot requires administrator privileges to start.\n\n" + "Please relaunch the application and approve the UAC prompt.\n\n" + "This instance will now close.", "Administrator Privileges Required", MessageBoxButton.OK, MessageBoxImage.Warning); } private void OnUnhandledException(object sender, UnhandledExceptionEventArgs e) { var exception = e.ExceptionObject as Exception ?? new InvalidOperationException("Unhandled non-Exception object was raised."); this.ReportUnhandledException(exception, "AppDomain.CurrentDomain.UnhandledException", LogLevel.Critical); var errorMessage = $"A critical error occurred:\n\n{exception?.Message}\n\nThe application will now exit."; System.Windows.MessageBox.Show(errorMessage, "Critical Error", MessageBoxButton.OK, MessageBoxImage.Error); } private void OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e) { this.ReportUnhandledException(e.Exception, "Application.DispatcherUnhandledException", LogLevel.Error); if (Interlocked.CompareExchange(ref this.uiExceptionDialogOpen, 1, 0) != 0) { e.Handled = true; return; } if (DateTime.UtcNow - this.lastUiExceptionDialogUtc < TimeSpan.FromSeconds(2)) { e.Handled = true; Interlocked.Exchange(ref this.uiExceptionDialogOpen, 0); return; } this.lastUiExceptionDialogUtc = DateTime.UtcNow; var errorMessage = $"An error occurred in the user interface:\n\n{e.Exception.Message}\n\nDo you want to continue?"; var result = System.Windows.MessageBox.Show(errorMessage, "UI Error", MessageBoxButton.YesNo, MessageBoxImage.Error); if (result == MessageBoxResult.Yes) { e.Handled = true; // Continue running } else { e.Handled = false; // Let the application crash } Interlocked.Exchange(ref this.uiExceptionDialogOpen, 0); } private void OnUnobservedTaskException(object? sender, UnobservedTaskExceptionEventArgs e) { var exception = e.Exception.Flatten(); this.ReportUnhandledException(exception, "TaskScheduler.UnobservedTaskException", LogLevel.Error); e.SetObserved(); } private void ReportUnhandledException(Exception exception, string source, LogLevel level) { var logger = this.ServiceProvider?.GetService>(); if (level == LogLevel.Critical) { logger?.LogCritical(exception, "Unhandled exception in {Source}", source); } else { logger?.LogError(exception, "Unhandled exception in {Source}", source); } var enhancedLogger = this.ServiceProvider?.GetService(); if (enhancedLogger == null) { return; } var errorCode = exception is ThreadPilotException typedException ? typedException.ErrorCode.ToString() : ErrorCode.Unhandled.ToString(); var context = new Dictionary { ["Source"] = source, [LogProperties.ErrorCode] = errorCode, [LogProperties.CorrelationId] = enhancedLogger.GetCurrentCorrelationId() ?? "N/A", ["IsTerminatingLevel"] = level == LogLevel.Critical, }; TaskSafety.FireAndForget( enhancedLogger.LogErrorAsync(exception, source, context), logFailure => logger?.LogWarning(logFailure, "Failed to persist unhandled exception report")); } } }