|
| 1 | +using Microsoft.Extensions.DependencyInjection; |
| 2 | +using Microsoft.Extensions.Hosting; |
| 3 | +using System; |
| 4 | +using System.IO; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using System.Windows; |
| 7 | +using System.Windows.Threading; |
| 8 | +using TensorStack.WPF; |
| 9 | +using TensorStack.Example.Services; |
| 10 | + |
| 11 | +namespace TensorStack.Example.Upscaler |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// Interaction logic for App.xaml |
| 15 | + /// </summary> |
| 16 | + public partial class App : Application |
| 17 | + { |
| 18 | + private readonly IHost _appHost; |
| 19 | + |
| 20 | + public App() |
| 21 | + { |
| 22 | + RegisterExceptionHandlers(); |
| 23 | + |
| 24 | + var builder = Host.CreateApplicationBuilder(); |
| 25 | + |
| 26 | + var configuration = Json.Load<Settings>("Settings.json"); |
| 27 | + configuration.Initialize(); |
| 28 | + |
| 29 | + // Add WPFCommon |
| 30 | + builder.Services.AddWPFCommon<MainWindow, Settings>(configuration); |
| 31 | + |
| 32 | + builder.Services.AddSingleton<IMediaService, MediaService>(); |
| 33 | + builder.Services.AddSingleton<IUpscaleService, UpscaleService>(); |
| 34 | + builder.Services.AddSingleton<IInterpolationService, InterpolationService>(); |
| 35 | + |
| 36 | + _appHost = builder.Build(); |
| 37 | + |
| 38 | + // Initialize WPFCommon |
| 39 | + _appHost.Services.UseWPFCommon(); |
| 40 | + } |
| 41 | + |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// Application startup. |
| 45 | + /// </summary> |
| 46 | + /// <returns>Task.</returns> |
| 47 | + private Task AppStartup() |
| 48 | + { |
| 49 | + MainWindow = _appHost.Services.GetMainWindow(); |
| 50 | + MainWindow.Show(); |
| 51 | + return Task.CompletedTask; |
| 52 | + } |
| 53 | + |
| 54 | + |
| 55 | + /// <summary> |
| 56 | + /// Application shutdown. |
| 57 | + /// </summary> |
| 58 | + private async Task AppShutdown() |
| 59 | + { |
| 60 | + using (_appHost) |
| 61 | + { |
| 62 | + await _appHost.StopAsync(); |
| 63 | + DeregisterExceptionHandlers(); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + |
| 68 | + /// <summary> |
| 69 | + /// Raises the <see cref="E:System.Windows.Application.Startup" /> event. |
| 70 | + /// </summary> |
| 71 | + /// <param name="e">A <see cref="T:System.Windows.StartupEventArgs" /> that contains the event data.</param> |
| 72 | + protected override async void OnStartup(StartupEventArgs e) |
| 73 | + { |
| 74 | + await AppStartup(); |
| 75 | + base.OnStartup(e); |
| 76 | + } |
| 77 | + |
| 78 | + |
| 79 | + /// <summary> |
| 80 | + /// Raises the <see cref="E:System.Windows.Application.SessionEnding" /> event. |
| 81 | + /// </summary> |
| 82 | + /// <param name="e">A <see cref="T:System.Windows.SessionEndingCancelEventArgs" /> that contains the event data.</param> |
| 83 | + protected override async void OnSessionEnding(SessionEndingCancelEventArgs e) |
| 84 | + { |
| 85 | + await AppShutdown(); |
| 86 | + base.OnSessionEnding(e); |
| 87 | + } |
| 88 | + |
| 89 | + |
| 90 | + /// <summary> |
| 91 | + /// Raises the <see cref="E:System.Windows.Application.Exit" /> event. |
| 92 | + /// </summary> |
| 93 | + /// <param name="e">An <see cref="T:System.Windows.ExitEventArgs" /> that contains the event data.</param> |
| 94 | + protected async override void OnExit(ExitEventArgs e) |
| 95 | + { |
| 96 | + await AppShutdown(); |
| 97 | + base.OnExit(e); |
| 98 | + } |
| 99 | + |
| 100 | + |
| 101 | + /// <summary> |
| 102 | + /// Registers the exception handlers. |
| 103 | + /// </summary> |
| 104 | + private void RegisterExceptionHandlers() |
| 105 | + { |
| 106 | + DispatcherUnhandledException += OnDispatcherException; |
| 107 | + AppDomain.CurrentDomain.UnhandledException += OnAppDomainException; |
| 108 | + TaskScheduler.UnobservedTaskException += OnTaskSchedulerException; |
| 109 | + } |
| 110 | + |
| 111 | + |
| 112 | + /// <summary> |
| 113 | + /// Deregisters the exception handlers. |
| 114 | + /// </summary> |
| 115 | + private void DeregisterExceptionHandlers() |
| 116 | + { |
| 117 | + DispatcherUnhandledException -= OnDispatcherException; |
| 118 | + AppDomain.CurrentDomain.UnhandledException -= OnAppDomainException; |
| 119 | + TaskScheduler.UnobservedTaskException -= OnTaskSchedulerException; |
| 120 | + } |
| 121 | + |
| 122 | + |
| 123 | + /// <summary> |
| 124 | + /// Handles the <see cref="E:DispatcherException" /> event. |
| 125 | + /// </summary> |
| 126 | + /// <param name="sender">The sender.</param> |
| 127 | + /// <param name="e">The <see cref="DispatcherUnhandledExceptionEventArgs"/> instance containing the event data.</param> |
| 128 | + private void OnDispatcherException(object sender, DispatcherUnhandledExceptionEventArgs e) |
| 129 | + { |
| 130 | + ShowExceptionMessage(e.Exception); |
| 131 | + |
| 132 | + // Prevent application from crashing |
| 133 | + e.Handled = true; |
| 134 | + } |
| 135 | + |
| 136 | + |
| 137 | + /// <summary> |
| 138 | + /// Handles the <see cref="E:AppDomainException" /> event. |
| 139 | + /// </summary> |
| 140 | + /// <param name="sender">The sender.</param> |
| 141 | + /// <param name="e">The <see cref="UnhandledExceptionEventArgs"/> instance containing the event data.</param> |
| 142 | + private void OnAppDomainException(object sender, UnhandledExceptionEventArgs e) |
| 143 | + { |
| 144 | + if (e.ExceptionObject is Exception ex) |
| 145 | + { |
| 146 | + ShowExceptionMessage(ex); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + |
| 151 | + /// <summary> |
| 152 | + /// Handles the <see cref="E:TaskSchedulerException" /> event. |
| 153 | + /// </summary> |
| 154 | + /// <param name="sender">The sender.</param> |
| 155 | + /// <param name="e">The <see cref="UnobservedTaskExceptionEventArgs"/> instance containing the event data.</param> |
| 156 | + private void OnTaskSchedulerException(object sender, UnobservedTaskExceptionEventArgs e) |
| 157 | + { |
| 158 | + ShowExceptionMessage(e.Exception); |
| 159 | + |
| 160 | + // Prevent application from crashing |
| 161 | + e.SetObserved(); |
| 162 | + } |
| 163 | + |
| 164 | + |
| 165 | + private void ShowExceptionMessage(Exception ex) |
| 166 | + { |
| 167 | + MessageBox.Show($"An unexpected error occurred:\n{ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error); |
| 168 | + } |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | + |
0 commit comments