diff --git a/DS4Windows/App.xaml.cs b/DS4Windows/App.xaml.cs index 018095c..e07647c 100644 --- a/DS4Windows/App.xaml.cs +++ b/DS4Windows/App.xaml.cs @@ -128,6 +128,7 @@ namespace DS4WinWPF logHolder = new LoggerHolder(rootHub); DispatcherUnhandledException += App_DispatcherUnhandledException; + AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; Logger logger = logHolder.Logger; string version = DS4Windows.Global.exeversion; logger.Info($"DS4Windows version {version}"); @@ -173,6 +174,26 @@ namespace DS4WinWPF window.LateChecks(parser); } + private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) + { + if (!Current.Dispatcher.CheckAccess()) + { + Logger logger = logHolder.Logger; + Exception exp = e.ExceptionObject as Exception; + logger.Error($"Thread App Crashed with message {exp.Message}"); + logger.Error(exp.ToString()); + //LogManager.Flush(); + //LogManager.Shutdown(); + if (e.IsTerminating) + { + Dispatcher.Invoke(() => + { + CleanShutdown(1); + }); + } + } + } + private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e) { //Console.WriteLine("App Crashed"); @@ -450,7 +471,7 @@ namespace DS4WinWPF CleanShutdown(); } - private void CleanShutdown() + private void CleanShutdown(int exitCode = 0) { if (runShutdown) { @@ -458,7 +479,8 @@ namespace DS4WinWPF { Task.Run(() => { - rootHub.Stop(); + if (rootHub.running) + rootHub.Stop(); }).Wait(); } @@ -481,6 +503,11 @@ namespace DS4WinWPF LogManager.Flush(); LogManager.Shutdown(); + + if (exitCode != 0) + { + Current.Shutdown(exitCode); + } } } } diff --git a/DS4Windows/DS4Library/DS4Device.cs b/DS4Windows/DS4Library/DS4Device.cs index afa3ed6..b38a1cc 100644 --- a/DS4Windows/DS4Library/DS4Device.cs +++ b/DS4Windows/DS4Library/DS4Device.cs @@ -608,8 +608,10 @@ namespace DS4Windows try { exitInputThread = true; - //ds4Input.Abort(); - ds4Input.Join(); + if (ds4Input.ThreadState.HasFlag(System.Threading.ThreadState.WaitSleepJoin)) + ds4Input.Interrupt(); + else + ds4Input.Join(); } catch (Exception e) {