From c191b116a234e4eeebd852f05d537cc67f7b3f74 Mon Sep 17 00:00:00 2001 From: neuecc Date: Sun, 7 Jan 2024 01:15:51 +0900 Subject: [PATCH] SetDefaultObservableSystem --- sandbox/AvaloniaApplication1/MainWindow.axaml.cs | 3 ++- sandbox/AvaloniaApplication1/Program.cs | 3 ++- sandbox/WpfApp1/MainWindow.xaml.cs | 2 +- .../AppBuilderR3InitializeExtensions.cs | 16 ++++++++-------- src/R3.Avalonia/AvaloniaProviderInitializer.cs | 12 ++++++++---- src/R3.WPF/WpfProviderInitializer.cs | 12 ++++++++---- 6 files changed, 29 insertions(+), 19 deletions(-) diff --git a/sandbox/AvaloniaApplication1/MainWindow.axaml.cs b/sandbox/AvaloniaApplication1/MainWindow.axaml.cs index 1a9555eb..ec94da23 100644 --- a/sandbox/AvaloniaApplication1/MainWindow.axaml.cs +++ b/sandbox/AvaloniaApplication1/MainWindow.axaml.cs @@ -1,4 +1,5 @@ using Avalonia.Controls; +using Avalonia.Logging; using R3; using System; using System.Diagnostics; @@ -15,7 +16,7 @@ public MainWindow() - + diff --git a/sandbox/AvaloniaApplication1/Program.cs b/sandbox/AvaloniaApplication1/Program.cs index fbb776e5..46bcce9a 100644 --- a/sandbox/AvaloniaApplication1/Program.cs +++ b/sandbox/AvaloniaApplication1/Program.cs @@ -1,4 +1,5 @@ using Avalonia; +using Avalonia.Logging; using System; namespace AvaloniaApplication1; @@ -18,5 +19,5 @@ public static AppBuilder BuildAvaloniaApp() .UsePlatformDetect() .WithInterFont() .LogToTrace() - .UseR3(); // add this line + .UseR3(ex => Logger.Sink?.Log(LogEventLevel.Error, "R3", null, "R3 Unhandled Exception {0}", ex)); // add this line } diff --git a/sandbox/WpfApp1/MainWindow.xaml.cs b/sandbox/WpfApp1/MainWindow.xaml.cs index 35d893e7..7ad02e2b 100644 --- a/sandbox/WpfApp1/MainWindow.xaml.cs +++ b/sandbox/WpfApp1/MainWindow.xaml.cs @@ -18,7 +18,7 @@ public MainWindow() - R3.WPF.WpfProviderInitializer.SetDefaultProviders(); + R3.WPF.WpfProviderInitializer.SetDefaultObservableSystem(ex => Trace.WriteLine($"R3 UnhandledException:{ex}")); diff --git a/src/R3.Avalonia/AppBuilderR3InitializeExtensions.cs b/src/R3.Avalonia/AppBuilderR3InitializeExtensions.cs index 90998ee8..bc6f1804 100644 --- a/src/R3.Avalonia/AppBuilderR3InitializeExtensions.cs +++ b/src/R3.Avalonia/AppBuilderR3InitializeExtensions.cs @@ -5,24 +5,24 @@ namespace Avalonia; // Avalonia namespace public static class AppBuilderR3InitializeExtensions { - public static AppBuilder UseR3(this AppBuilder builder) + public static AppBuilder UseR3(this AppBuilder builder, Action unhandledExceptionHandler) { // need to delay setup, initialize provider(dispatcher) need to determine platform - return builder.AfterSetup(_ => AvaloniaProviderInitializer.SetDefaultProviders()); + return builder.AfterSetup(_ => AvaloniaProviderInitializer.SetDefaultObservableSystem(unhandledExceptionHandler)); } - public static AppBuilder UseR3(this AppBuilder builder, DispatcherPriority priority) + public static AppBuilder UseR3(this AppBuilder builder, DispatcherPriority priority, Action unhandledExceptionHandler) { - return builder.AfterSetup(_ => AvaloniaProviderInitializer.SetDefaultProviders(priority)); + return builder.AfterSetup(_ => AvaloniaProviderInitializer.SetDefaultObservableSystem(unhandledExceptionHandler,priority)); } - public static AppBuilder UseR3(this AppBuilder builder, int framesPerSecond) + public static AppBuilder UseR3(this AppBuilder builder, int framesPerSecond, Action unhandledExceptionHandler) { - return builder.AfterSetup(_ => AvaloniaProviderInitializer.SetDefaultProviders(framesPerSecond)); + return builder.AfterSetup(_ => AvaloniaProviderInitializer.SetDefaultObservableSystem(unhandledExceptionHandler,framesPerSecond)); } - public static AppBuilder UseR3(this AppBuilder builder, DispatcherPriority priority, int framesPerSecond) + public static AppBuilder UseR3(this AppBuilder builder, DispatcherPriority priority, int framesPerSecond, Action unhandledExceptionHandler) { - return builder.AfterSetup(_ => AvaloniaProviderInitializer.SetDefaultProviders(priority, framesPerSecond)); + return builder.AfterSetup(_ => AvaloniaProviderInitializer.SetDefaultObservableSystem(unhandledExceptionHandler,priority, framesPerSecond)); } } diff --git a/src/R3.Avalonia/AvaloniaProviderInitializer.cs b/src/R3.Avalonia/AvaloniaProviderInitializer.cs index 7b944fdc..92470e4f 100644 --- a/src/R3.Avalonia/AvaloniaProviderInitializer.cs +++ b/src/R3.Avalonia/AvaloniaProviderInitializer.cs @@ -4,26 +4,30 @@ namespace R3.Avalonia; public static class AvaloniaProviderInitializer { - public static void SetDefaultProviders() + public static void SetDefaultObservableSystem(Action unhandledExceptionHandler) { + ObservableSystem.RegisterUnhandledExceptionHandler(unhandledExceptionHandler); ObservableSystem.DefaultTimeProvider = new AvaloniaDispatcherTimerProvider(); ObservableSystem.DefaultFrameProvider = new AvaloniaDispatcherFrameProvider(); } - public static void SetDefaultProviders(DispatcherPriority priority) + public static void SetDefaultObservableSystem(Action unhandledExceptionHandler, DispatcherPriority priority) { + ObservableSystem.RegisterUnhandledExceptionHandler(unhandledExceptionHandler); ObservableSystem.DefaultTimeProvider = new AvaloniaDispatcherTimerProvider(priority); ObservableSystem.DefaultFrameProvider = new AvaloniaDispatcherFrameProvider(priority); } - public static void SetDefaultProviders(int framesPerSecond) + public static void SetDefaultObservableSystem(Action unhandledExceptionHandler, int framesPerSecond) { + ObservableSystem.RegisterUnhandledExceptionHandler(unhandledExceptionHandler); ObservableSystem.DefaultTimeProvider = new AvaloniaDispatcherTimerProvider(); ObservableSystem.DefaultFrameProvider = new AvaloniaDispatcherFrameProvider(framesPerSecond); } - public static void SetDefaultProviders(DispatcherPriority priority, int framesPerSecond) + public static void SetDefaultObservableSystem(Action unhandledExceptionHandler, DispatcherPriority priority, int framesPerSecond) { + ObservableSystem.RegisterUnhandledExceptionHandler(unhandledExceptionHandler); ObservableSystem.DefaultTimeProvider = new AvaloniaDispatcherTimerProvider(priority); ObservableSystem.DefaultFrameProvider = new AvaloniaDispatcherFrameProvider(framesPerSecond, priority); } diff --git a/src/R3.WPF/WpfProviderInitializer.cs b/src/R3.WPF/WpfProviderInitializer.cs index 0399393b..afc3c4be 100644 --- a/src/R3.WPF/WpfProviderInitializer.cs +++ b/src/R3.WPF/WpfProviderInitializer.cs @@ -1,23 +1,27 @@ -using System.Windows.Threading; +using System.Windows; +using System.Windows.Threading; namespace R3.WPF; public static class WpfProviderInitializer { - public static void SetDefaultProviders() + public static void SetDefaultObservableSystem(Action unhandledExceptionHandler) { + ObservableSystem.RegisterUnhandledExceptionHandler(unhandledExceptionHandler); ObservableSystem.DefaultTimeProvider = new WpfDispatcherTimerProvider(); ObservableSystem.DefaultFrameProvider = new WpfRenderingFrameProvider(); } - public static void SetDefaultProviders(DispatcherPriority priority) + public static void SetDefaultObservableSystem(Action unhandledExceptionHandler, DispatcherPriority priority) { + ObservableSystem.RegisterUnhandledExceptionHandler(unhandledExceptionHandler); ObservableSystem.DefaultTimeProvider = new WpfDispatcherTimerProvider(priority); ObservableSystem.DefaultFrameProvider = new WpfRenderingFrameProvider(); } - public static void SetDefaultProviders(DispatcherPriority priority, Dispatcher dispatcher) + public static void SetDefaultObservableSystem(Action unhandledExceptionHandler, DispatcherPriority priority, Dispatcher dispatcher) { + ObservableSystem.RegisterUnhandledExceptionHandler(unhandledExceptionHandler); ObservableSystem.DefaultTimeProvider = new WpfDispatcherTimerProvider(priority, dispatcher); ObservableSystem.DefaultFrameProvider = new WpfRenderingFrameProvider(); }