Question

I have been following the guidance here FileSystemWatcher Changed event is raised twice.

However I have a list of files that I'm watching so if I delete 20 files together the event is called 20 times. This is expected and works fine.

How can I only have an event fired once for 20 "simultaneous" file changes (i.e How can I ignore all other file changes until the code in the first instance of Onchanged below has completed. Right now Onchanged is called 20 times.) ?

private void Main_Load(object sender, EventArgs e)
{
    public static List<FileSystemWatcher> watchers = new List<FileSystemWatcher>();
    UpdateWatcher();
}


public void OnChanged(object sender, FileSystemEventArgs e)
{
    try
    {
      Logging.Write_To_Log_File("Item change detected " + e.ChangeType + " " + e.FullPath + " " + e.Name, MethodBase.GetCurrentMethod().Name, "", "", "", "", "", "", 2);
      watchers.Clear();

      foreach (FileSystemWatcher element in MyGlobals.watchers)
      {
        element.EnableRaisingEvents = false;
      }

      //Do some processing on my list of files here....
      return;

    }    
    catch (Exception ex)
    {
       // If exception happens, it will be returned here
    }                
    finally
    {
         foreach (FileSystemWatcher element in MyGlobals.watchers)
         {
            element.EnableRaisingEvents = true;
         }
    }
}





public void UpdateWatcher() // Check Items
        {

        try
        {
        watchers.Clear();

        for (int i = 0; i < MyGlobals.ListOfItemsToControl.Count; i++) // Loop through List with for
        {
        FileSystemWatcher w = new FileSystemWatcher();
         w.Path = Path.GetDirectoryName(MyGlobals.ListOfItemsToControl[i].sItemName); // File path    
        w.Filter = Path.GetFileName(MyGlobals.ListOfItemsToControl[i].sItemName); // File name
        w.Changed += new FileSystemEventHandler(OnChanged);
        w.Deleted += new FileSystemEventHandler(OnChanged);
        w.Created += new FileSystemEventHandler(OnChanged);
        w.EnableRaisingEvents = true;
        watchers.Add(w);
        }
        }
        catch (Exception ex)
        {
        // If exception happens, it will be returned here

        }
        }
Was it helpful?

Solution

The key point here is what does "together" mean to you. after all the system does an independent delete operation for each, which would technically mean they are not all deleted at the exact same time, but if you just wanna be close, let's say if they are all deleted within 5 seconds of each other then we only want OnChange to fire once, you can do the following. Note that this doesn't handle the rename change notification. You weren't listening for it, so I assumed you don't need to.

you may wanna change the 5 seconds window to a small window depending on your use.

    class SlowFileSystemWatcher : FileSystemWatcher
    {
        public delegate void SlowFileSystemWatcherEventHandler(object sender, FileSystemEventArgs args);

        public event SlowFileSystemWatcherEventHandler SlowChanged;
        public DateTime LastFired { get; private set; }

        public SlowFileSystemWatcher(string path)
            : base(path)
        {
            Changed += HandleChange;
            Created += HandleChange;
            Deleted += HandleChange;
            LastFired = DateTime.MinValue;
        }

        private void SlowGeneralChange(object sender, FileSystemEventArgs args)
        {
            if (LastFired.Add(TimeSpan.FromSeconds(5)) < DateTime.UtcNow)
            {
                SlowChanged.Invoke(sender, args);
                LastFired = DateTime.UtcNow;
            }
        }

        private void HandleChange(object sender, FileSystemEventArgs args)
        {
            SlowGeneralChange(sender, args);
        }

        protected override void Dispose(bool disposing)
        {
            SlowChanged = null;
            base.Dispose(disposing);
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top