Exemple #1
0
 private void watcher_Changed(object sender, FileSystemEventArgs e)
 {
     try
     {
         if (e.ChangeType == WatcherChangeTypes.Changed &&
             IsSamePath(this._fileName, e.FullPath))
         {
             Debug.WriteLine("### File changed " + this._fileName);
             // Apart from retrying, the DelayedActions has the nice side effect of also
             // collapsing multiple file system events into one action callback.
             var change = new FileChangedActions()
             {
                 OrignalName = this._fileName, NewName = e.FullPath, Cache = this, ChangeType = e.ChangeType
             };
             if (pending == null)
             {
                 _retries = 5;
                 pending  = change;
                 _actions.StartDelayedAction("reload", () => pending.HandleReload(), TimeSpan.FromSeconds(1));
             }
             else
             {
                 pending.Add(change);
             }
         }
     }
     catch { }
 }
Exemple #2
0
 private void watcher_Renamed(object sender, RenamedEventArgs e)
 {
     // Some editors rename the file to *.bak then save the new version and
     // in that case we do not want XmlNotepad to switch to the .bak file.
     try
     {
         string ext = Path.GetExtension(e.FullPath);
         if (IsSamePath(this._fileName, e.OldFullPath) ||
             IsSamePath(this._fileName, e.FullPath))
         {
             var change = new FileChangedActions()
             {
                 OrignalName = this._fileName, OldName = e.OldFullPath, NewName = e.FullPath, Cache = this, ChangeType = e.ChangeType
             };
             if (pending == null)
             {
                 _retries = 5;
                 pending  = change;
                 _actions.StartDelayedAction("reload", () => pending.HandleReload(), TimeSpan.FromSeconds(1));
             }
             else
             {
                 pending.Add(change);
             }
         }
     } catch
     {}
 }
Exemple #3
0
        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);
                }
            }
        }