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