void StartReload() { // Apart from retrying, the DelayedActions has the nice side effect of also // collapsing multiple file system events into one action callback. _retries = 3; string temp = this._fileName; // capture file name in case it changes. _actions.StartDelayedAction("reload", () => { CheckReload(temp); }, TimeSpan.FromSeconds(1)); }
internal void CheckReload(FileChangedActions action, string fileName) { if (!File.Exists(fileName)) { // file was deleted... return; } pending = null; try { // Only do the reload if the file on disk really is different from // what we last loaded. if (this._lastModified < LastModTime && this._fileName == fileName) { // Test if we can open the file (it might still be locked). using (FileStream fs = new FileStream(this._fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { fs.Close(); } FireFileChanged(); } } catch (Exception ex) { _retries--; if (_retries > 0) { Debug.WriteLine("Retrying after FileStream error: " + ex.Message); // perhaps the file is still locked by the writer, so try again in a bit. _actions.StartDelayedAction("reload", () => action.HandleReload(), TimeSpan.FromSeconds(1)); } else { Debug.WriteLine("Giving up after FileStream error: " + ex.Message); } } }