public MouseUtilsPage() { try { // By mistake, the first release of Find My Mouse was saving settings in two places at the same time. // Delete the wrong path for Find My Mouse settings. var tempSettingsUtils = new SettingsUtils(); if (tempSettingsUtils.SettingsExists("Find My Mouse")) { var settingsFilePath = tempSettingsUtils.GetSettingsFilePath("Find My Mouse"); System.IO.File.Delete(settingsFilePath); tempSettingsUtils.DeleteSettings("Find My Mouse"); } } #pragma warning disable CA1031 // Do not catch general exception types catch (System.Exception) #pragma warning restore CA1031 // Do not catch general exception types { } var settingsUtils = new SettingsUtils(); ViewModel = new MouseUtilsViewModel( settingsUtils, SettingsRepository <GeneralSettings> .GetInstance(settingsUtils), SettingsRepository <FindMyMouseSettings> .GetInstance(settingsUtils), SettingsRepository <MouseHighlighterSettings> .GetInstance(settingsUtils), SettingsRepository <MousePointerCrosshairsSettings> .GetInstance(settingsUtils), ShellPage.SendDefaultIPCMessage); DataContext = ViewModel; InitializeComponent(); }
//worker thread //------------- private void backgroundWorkerConnect_DoWork(object sender, DoWorkEventArgs e) { DeviceSettings sessionInfo = (DeviceSettings)e.Argument; try { _connecting = true; _connected = _session.OpenSession(sessionInfo.Host, sessionInfo.Username, sessionInfo.Password); //check remember settings if (_connected) { if (checkBoxSave.Checked) { SettingsUtils.SaveSettings(new ToolSettings { Device = _selectedModem, Username = sessionInfo.Username, Password = sessionInfo.Password, Host = sessionInfo.Host, }); } else { SettingsUtils.DeleteSettings(); } //refresh ThreadUtils.SetButtonTextFromThread(buttonConnect, "Refreshing, please wait..."); _session.RefreshData(); } } catch (Exception ex) { MessageBox.Show("Unexpected error occurred. Debug info: " + ex, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public void OverloadSettings() { Monitor.Enter(_watcherSyncObject); var retry = true; var retryCount = 0; while (retry) { try { retryCount++; CreateSettingsIfNotExists(); var overloadSettings = SettingsUtils.GetSettings <PowerLauncherSettings>(PowerLauncherSettings.ModuleName); var openPowerlauncher = ConvertHotkey(overloadSettings.Properties.OpenPowerLauncher); if (_settings.Hotkey != openPowerlauncher) { _settings.Hotkey = openPowerlauncher; } var shell = PluginManager.AllPlugins.Find(pp => pp.Metadata.Name == "Shell"); if (shell != null) { var shellSettings = shell.Plugin as ISettingProvider; shellSettings.UpdateSettings(overloadSettings); } if (_settings.MaxResultsToShow != overloadSettings.Properties.MaximumNumberOfResults) { _settings.MaxResultsToShow = overloadSettings.Properties.MaximumNumberOfResults; } if (_settings.IgnoreHotkeysOnFullscreen != overloadSettings.Properties.IgnoreHotkeysInFullscreen) { _settings.IgnoreHotkeysOnFullscreen = overloadSettings.Properties.IgnoreHotkeysInFullscreen; } var indexer = PluginManager.AllPlugins.Find(p => p.Metadata.Name.Equals("Windows Indexer", StringComparison.OrdinalIgnoreCase)); if (indexer != null) { var indexerSettings = indexer.Plugin as ISettingProvider; indexerSettings.UpdateSettings(overloadSettings); } if (_settings.ClearInputOnLaunch != overloadSettings.Properties.ClearInputOnLaunch) { _settings.ClearInputOnLaunch = overloadSettings.Properties.ClearInputOnLaunch; } retry = false; } // the settings application can hold a lock on the settings.json file which will result in a IOException. // This should be changed to properly synch with the settings app instead of retrying. catch (IOException e) { if (retryCount > MaxRetries) { retry = false; Log.Exception($"|SettingsWatcher.OverloadSettings| Failed to Deserialize PowerToys settings, Retrying {e.Message}", e); } else { Thread.Sleep(1000); } } catch (JsonException e) { if (retryCount > MaxRetries) { retry = false; Log.Exception($"|SettingsWatcher.OverloadSettings| Failed to Deserialize PowerToys settings, Creating new settings as file could be corrupted {e.Message}", e); // Settings.json could possibly be corrupted. To mitigate this we delete the // current file and replace it with a correct json value. SettingsUtils.DeleteSettings(PowerLauncherSettings.ModuleName); CreateSettingsIfNotExists(); ErrorReporting.ShowMessageBox(Properties.Resources.deseralization_error_title, Properties.Resources.deseralization_error_message); } else { Thread.Sleep(1000); } } } Monitor.Exit(_watcherSyncObject); }