diff --git a/Rapr/Program.cs b/Rapr/Program.cs index 18c4dae..9ef762c 100644 --- a/Rapr/Program.cs +++ b/Rapr/Program.cs @@ -4,6 +4,7 @@ using System.IO; using System.Reflection; using System.Security; +using System.Threading; using System.Windows.Forms; using Rapr.Lang; @@ -72,6 +73,25 @@ private static void CleanUpOldConfig() } } + internal static void LogUnhandledException(object exception) + { + Trace.TraceError($"Unhandled exception: {exception}"); + } + + private static void OnThreadException(object sender, ThreadExceptionEventArgs e) + { + LogUnhandledException(e.Exception); + + // Same dialog WinForms shows when there is no handler. + using (var dialog = new ThreadExceptionDialog(e.Exception)) + { + if (dialog.ShowDialog() == DialogResult.Abort) + { + Application.Exit(); + } + } + } + /// /// The main entry point for the application. /// @@ -85,6 +105,11 @@ public static void Main() Trace.IndentSize = 4; Trace.Listeners.Add(new TextFileTraceListener()); + // Write crashes to Rapr.log; otherwise a crash during startup leaves nothing to report. + Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); + Application.ThreadException += OnThreadException; + AppDomain.CurrentDomain.UnhandledException += (sender, e) => LogUnhandledException(e.ExceptionObject); + AddEnvironmentPaths(@"C:\Windows\System32\CompatTel"); UpdateManager.CleanUpOldFiles();