Exemplo n.º 1
0
        public void SaveNow()
        {
            _timer.Stop();             //Resets the timer to the start of the interval

            if (!SettingsDirty)
            {
                return;
            }

            var inProgressSavePath = Path.Combine(SettingsFolder, SettingsLoader.IncompleteSettingsFileName);
            var oldSavePath        = Path.Combine(SettingsFolder, SettingsLoader.OldSettingsFileName);
            var savePath           = Path.Combine(SettingsFolder, SettingsLoader.SettingsFileName);

            //This process, along with the fact that the loader checks for a file at the "old save path"
            //if one at the normal save path isn't found, ensures that there is always a save file available,
            //so it should never get corrupted/lost or anything.
            SettingsHost.ToRaw().ToXml().Save(inProgressSavePath);

            if (File.Exists(savePath))
            {
                File.Move(savePath, oldSavePath);
            }
            File.Move(inProgressSavePath, savePath);
            File.Delete(oldSavePath);

            SettingsDirty = false;
            _logger.Log("Saved all settings.", LogType.MinorMessage);
        }
Exemplo n.º 2
0
        public SettingsSaver(SettingsHost settingsHost, string settingsFolder, int saveDelay, Logger logger)
        {
            if (saveDelay <= 0)
            {
                throw new ArgumentOutOfRangeException("Must be greater than zero.");
            }

            this.SettingsHost   = settingsHost ?? throw new ArgumentNullException(nameof(settingsHost));
            this.SettingsFolder = settingsFolder ?? throw new ArgumentNullException(nameof(settingsFolder));
            this.SaveDelay      = saveDelay;
            this._logger        = logger;

            this._timer = new Timer()
            {
                Interval = saveDelay
            };
            this._timer.Tick += delegate { SaveNow(); };

            this.SettingsDirty = true;

            settingsHost.AnySettingChanged += delegate { SettingsDirty = true; };
        }