GH_FileWatcher multiple recomputes

Hi All,

I’m working on a (what seemed to be simple) component that fires up a GH_FileWatcher to check if a file has been changed and expire the solution and recompute if it did. For testing purposes I’ve simplified it to this:

  private void RunScript(string filePath, bool create, bool dispose, ref object A)
  {
    if(create){
      //create watcher
      CreateWatcher(filePath);
    }
    if(dispose){
      //dispose all watchers
      DestroyFileWatchers();
    }

    i = _fileWatchers.Values.Count();
    Print(i.ToString() + " FileWatchers active");

    A = "Something changed";
  }

  private static readonly Dictionary<string, GH_FileWatcher> _fileWatchers = new Dictionary<string, GH_FileWatcher>();
  private int i;

  private void CreateWatcher(string fldr)
  {
    try
    {
      if (!File.Exists(fldr))
      {
        MessageBox.Show("file doesn't exist: " + fldr);
        return;
      }
      _fileWatchers[fldr] = GH_FileWatcher.CreateFileWatcher(fldr, GH_FileWatcherEvents.Changed, new GH_FileWatcher.FileChangedSimple(fileChanged));
    }
    catch
    {
    }
  }

  private static void DestroyFileWatchers()
  {
    foreach (GH_FileWatcher ghFileWatcher in _fileWatchers.Values){
      if (ghFileWatcher != null){
        ghFileWatcher.Dispose();
      }
    }
    _fileWatchers.Clear();
  }

  private void fileChanged(string filePath){
    //cause the component to fire a new solution.
    Component.ExpireSolution(true);
  }

It does create the watcher and it also disposes it fine. But when I have (what I think is one) a watcher active, it expires the solution multiple times (usually two). Is there a way to check all active watchers? And did anyone else encounter this issue before?

Kind regards,

Merijn

Alright, you’ll always see that you find the answer when you’ve just posted the question on the forum…
I stumbled upon this: c# - FileSystemWatcher Changed event is raised twice - Stack Overflow

Posting the answer for anyone else facing this issue.

Fixed it by checking the last write time of the file. The event still gets fired twice, but won’t be handled:

  DateTime lastRead = DateTime.MinValue;

  private void fileChanged(string filePath){
    //cause the component to fire a new solution.
    DateTime lastWriteTime = File.GetLastWriteTime(filePath);
    if (lastWriteTime != lastRead)
    {
      Component.ExpireSolution(true);
      lastRead = lastWriteTime;
    }
    // else discard the (duplicated) OnChanged event
  }

Hi @merijndeleur, I am currently facing a similar issue. I am trying to solve it using Python (I do not know C#) but I am not getting anywhere. I found a file watcher C# script, but for some reason I do not understand it is not consistent on reading changes (when saving the file once it runs six times, making the triggered script run six times also). I know it has been a long time since you found the solution, but do you happen to have the code somewhere you could share?

Thanks in advance!