I'm using filesystemwatcher for a program that will run on framework 1.1 and when a change is happened it sends 2 signals saying its changed. From research I'm aware this is just how windows works and theres nothing I can do to stop this with the fsw.
But as a work around I would like to make it so it accepts the first pulse, then locks off so the other one trying to call the method is ignored (or redirected?) as I've got a backup system with it and it is making 2 copies of all the files so it's something I really need to address. Elsewhere in the code is effected and I've managed to use timers to fix this by blocking off the timer as soon as its called however in this instance it'll get quite messy and I'm sure there has to be a cleaner solution.
code:
private static void GetCurrentJob()
{
///Lots of code that isn't relevant
}
private static void ProgramSwapMonitor(string ProgramChange)
{
// Create a new FileSystemWatcher and set its properties.
FileSystemWatcher watcher = new FileSystemWatcher
{
Path = ProgramChange,
/* Watch for changes in LastAccess and LastWrite times, and
the renaming of files or directories. */
NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName,
// Only watch this specific file
Filter = "dnocontainer.cfg"
};
// Add event handlers.
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.Deleted += new FileSystemEventHandler(OnChanged);
watcher.Renamed += new RenamedEventHandler(OnRenamed);
// Begin watching.
watcher.EnableRaisingEvents = true;
}
// Define the event handlers.
private static void OnChanged(object source, FileSystemEventArgs e)
{
//This disables the directory monitor, then changes the active job in its memory, then restarts the directory monitor so it can now monitor the new location, then removes the old watcherChanged instance so theres no duplicates.
fileSystemWatcher.EnableRaisingEvents = false;
GetCurrentJob(); //This is the method that needs to be only running once, but is made to run twice
MonitorDirectory(path);
fileSystemWatcher.Changed -= new FileSystemEventHandler(FileSystemWatcher_Changed);
}