0

On the code below when a file is saved on notepad the filesystem watcher event is triggered twice even though i have a Workaround in place. Any suggestions for a fix ?

public void OnChanged(object sender, FileSystemEventArgs e)
        {
            try
            {

                    if (MyGlobals.LastFired.Add(TimeSpan.FromSeconds(0)) < DateTime.UtcNow) 
                    {
                        MyGlobals.LastFired = DateTime.UtcNow;
                        System.Threading.Thread.Sleep(2000);
                        Logging.Write_To_Log_File("Item change detected " + e.ChangeType + " " + e.FullPath + " " + e.Name, MethodBase.GetCurrentMethod().Name, "", "", "", "", "", "", 2);


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

                        this.BeginInvoke((MethodInvoker)(() => CheckFilesAsync())); //     
                    }
                    else
                    {
                        Logging.Write_To_Log_File("Change within the time limit", MethodBase.GetCurrentMethod().Name, "", "", "", "", "", "", 1);
                    }       
            }

            catch (Exception ex)
            {
                // If exception happens, it will be returned here
            }

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


        }
Community
  • 1
  • 1
user1438082
  • 2,740
  • 10
  • 48
  • 82
  • possible duplicate of [FileSystemWatcher Changed event is raised twice](http://stackoverflow.com/questions/1764809/filesystemwatcher-changed-event-is-raised-twice) – Crono Mar 21 '14 at 20:36
  • @Crono if you follow the answer with the second most votes then i have already tried that and the code element.EnableRaisingEvents = false; is included above. The answer with the most votes does not actually have a solution. – user1438082 Mar 21 '14 at 20:43
  • @Crono Possibly what is happening here is that the second event gets raised before enableraisingevents is set to false – user1438082 Mar 21 '14 at 20:50

0 Answers0