From af40bdd1153ab6863f82edf196cd91574f55241f Mon Sep 17 00:00:00 2001 From: AntonyCorbett Date: Wed, 16 Jan 2019 05:14:04 +0000 Subject: [PATCH] Additional exception handling on startup --- OnlyM/App.xaml.cs | 40 ++++++++++++++++++++++++++++++++++------ SolutionInfo.cs | 2 +- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/OnlyM/App.xaml.cs b/OnlyM/App.xaml.cs index 30fcefcc0..ddd34e99b 100644 --- a/OnlyM/App.xaml.cs +++ b/OnlyM/App.xaml.cs @@ -5,6 +5,7 @@ using System.IO; using System.Threading; using System.Windows; + using System.Windows.Threading; using AutoUpdates; using CefSharp; using CefSharp.Wpf; @@ -29,14 +30,22 @@ public partial class App : Application public App() { - DispatcherHelper.Initialize(); - MediaElement.FFmpegDirectory = FMpegFolderName; - RegisterMappings(); + try + { + DispatcherHelper.Initialize(); + MediaElement.FFmpegDirectory = FMpegFolderName; + RegisterMappings(); - // pre-load the CefSharp assemblies otherwise 1st instantiation is too long. - System.Reflection.Assembly.Load("CefSharp.Wpf"); + // pre-load the CefSharp assemblies otherwise 1st instantiation is too long. + System.Reflection.Assembly.Load("CefSharp.Wpf"); - _successCefSharp = InitCef(); + _successCefSharp = InitCef(); + } + catch (Exception ex) + { + AddEventLogEntry(ex.Message); + Current.Shutdown(); + } } public static string FMpegFolderName { get; } = $"{AppDomain.CurrentDomain.BaseDirectory}\\FFmpeg"; @@ -56,6 +65,8 @@ protected override void OnStartup(StartupEventArgs e) else { ConfigureLogger(); + + Current.DispatcherUnhandledException += CurrentDispatcherUnhandledException; } if (!_successCefSharp) @@ -126,5 +137,22 @@ private bool InitCef() return Cef.Initialize(settings); } + + private void CurrentDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e) + { + // unhandled exceptions thrown from UI thread + e.Handled = true; + Log.Logger.Fatal(e.Exception, "Unhandled exception"); + Current.Shutdown(); + } + + private void AddEventLogEntry(string msg) + { + using (var eventLog = new EventLog("Application")) + { + eventLog.Source = "Application"; + eventLog.WriteEntry(msg, EventLogEntryType.Error); + } + } } } diff --git a/SolutionInfo.cs b/SolutionInfo.cs index 1c808a04a..97688f435 100644 --- a/SolutionInfo.cs +++ b/SolutionInfo.cs @@ -6,4 +6,4 @@ [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] -[assembly: AssemblyVersion("1.4.0.7")] \ No newline at end of file +[assembly: AssemblyVersion("1.4.0.8")] \ No newline at end of file