From eaac72b6969f59e975a571ea1c00ccf7945c577b Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Sat, 12 Apr 2025 10:52:59 +0800 Subject: [PATCH] Handle TaskSchedulerUnhandledException --- Flow.Launcher/App.xaml.cs | 10 ++++++++++ Flow.Launcher/Helper/ErrorReporting.cs | 9 +++++++++ 2 files changed, 19 insertions(+) diff --git a/Flow.Launcher/App.xaml.cs b/Flow.Launcher/App.xaml.cs index 89faa105e..a9cd1a8b9 100644 --- a/Flow.Launcher/App.xaml.cs +++ b/Flow.Launcher/App.xaml.cs @@ -153,6 +153,7 @@ namespace Flow.Launcher RegisterAppDomainExceptions(); RegisterDispatcherUnhandledException(); + RegisterTaskSchedulerUnhandledException(); var imageLoadertask = ImageLoader.InitializeAsync(); @@ -284,6 +285,15 @@ namespace Flow.Launcher AppDomain.CurrentDomain.UnhandledException += ErrorReporting.UnhandledExceptionHandle; } + /// + /// let exception throw as normal is better for Debug + /// + [Conditional("RELEASE")] + private static void RegisterTaskSchedulerUnhandledException() + { + TaskScheduler.UnobservedTaskException += ErrorReporting.TaskSchedulerUnobservedTaskException; + } + #endregion #region IDisposable diff --git a/Flow.Launcher/Helper/ErrorReporting.cs b/Flow.Launcher/Helper/ErrorReporting.cs index 5b79c520d..1787b1d91 100644 --- a/Flow.Launcher/Helper/ErrorReporting.cs +++ b/Flow.Launcher/Helper/ErrorReporting.cs @@ -1,4 +1,5 @@ using System; +using System.Threading.Tasks; using System.Windows.Threading; using NLog; using Flow.Launcher.Infrastructure; @@ -30,6 +31,14 @@ public static class ErrorReporting e.Handled = true; } + public static void TaskSchedulerUnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e) + { + //handle unobserved task exceptions + Report(e.Exception); + //prevent application exist, so the user can copy prompted error info + e.SetObserved(); + } + public static string RuntimeInfo() { var info =