From 524f2bf5dbd19b724c8b23fd2bab06c9f4d70302 Mon Sep 17 00:00:00 2001 From: Paul De Smul Date: Thu, 12 Sep 2024 22:41:00 +0200 Subject: [PATCH] Fix crash when saving peferences --- src/Resources/Controls/EnhancedDataGrid.cs | 14 +++++++++++--- src/Services/PreferencesService.cs | 10 +++++----- src/ViewModels/MainViewModel.cs | 13 ++++++++++--- src/ViewModels/SettingsViewModel.cs | 13 ++++++++++--- 4 files changed, 36 insertions(+), 14 deletions(-) diff --git a/src/Resources/Controls/EnhancedDataGrid.cs b/src/Resources/Controls/EnhancedDataGrid.cs index 04a84db1..7dcb21cc 100644 --- a/src/Resources/Controls/EnhancedDataGrid.cs +++ b/src/Resources/Controls/EnhancedDataGrid.cs @@ -1,6 +1,7 @@ //https://bengribaudo.com/blog/2012/03/14/1942/saving-restoring-wpf-datagrid-columns-size-sorting-and-order using COMPASS.Services; +using COMPASS.Tools; using Newtonsoft.Json; using System; using System.Collections; @@ -67,9 +68,16 @@ private void UpdateColumnInfo() prefsService.Preferences.UIState.SortDirection = sd.Direction; } - Properties.Settings.Default["DataGridCollumnInfo"] = json; - Properties.Settings.Default.Save(); - updatingColumnInfo = false; + try + { + Properties.Settings.Default["DataGridCollumnInfo"] = json; + Properties.Settings.Default.Save(); + updatingColumnInfo = false; + } + catch (Exception ex) + { + Logger.Error("Failed to save Preferences", ex); + } } protected override void OnColumnReordered(DataGridColumnEventArgs e) { diff --git a/src/Services/PreferencesService.cs b/src/Services/PreferencesService.cs index 36dd7bcd..4a6a0038 100644 --- a/src/Services/PreferencesService.cs +++ b/src/Services/PreferencesService.cs @@ -27,13 +27,13 @@ private PreferencesService() { } public void SavePreferences() { - Properties.Settings.Default.Save(); - - if (_preferences == null) return; //don't save when they aren't loaded - PreferencesDto dto = _preferences.ToDto(); - try { + Properties.Settings.Default.Save(); + + if (_preferences == null) return; //don't save when they aren't loaded + PreferencesDto dto = _preferences.ToDto(); + string tempFileName = PreferencesFilePath + ".tmp"; lock (writeLocker) diff --git a/src/ViewModels/MainViewModel.cs b/src/ViewModels/MainViewModel.cs index dd97ac44..8596eb46 100644 --- a/src/ViewModels/MainViewModel.cs +++ b/src/ViewModels/MainViewModel.cs @@ -47,9 +47,16 @@ private void UpgradeSettings() { if (Properties.Settings.Default.justUpdated) { - Properties.Settings.Default.Upgrade(); - Properties.Settings.Default.justUpdated = false; - Properties.Settings.Default.Save(); + try + { + Properties.Settings.Default.Upgrade(); + Properties.Settings.Default.justUpdated = false; + Properties.Settings.Default.Save(); + } + catch (Exception ex) + { + Logger.Error("Failed to Update & Save Preferences", ex); + } } } diff --git a/src/ViewModels/SettingsViewModel.cs b/src/ViewModels/SettingsViewModel.cs index 36000f88..89fe14bb 100644 --- a/src/ViewModels/SettingsViewModel.cs +++ b/src/ViewModels/SettingsViewModel.cs @@ -425,9 +425,16 @@ public static string CompassDataPath set { - Properties.Settings.Default.CompassDataPath = value; - EnvironmentVarsService.CompassDataPath = value; - Properties.Settings.Default.Save(); + try + { + Properties.Settings.Default.CompassDataPath = value; + EnvironmentVarsService.CompassDataPath = value; + Properties.Settings.Default.Save(); + } + catch (Exception ex) + { + Logger.Error("Failed to update Compass Data Path", ex); + } } }