2
public void startWatch()
{
    FileSystemWatcher watcher = new FileSystemWatcher();
    watcher.Path = Path.GetDirectoryName(_file);
    watcher.Filter = Path.GetFileName(_file);
    watcher.NotifyFilter = NotifyFilters.LastWrite;
    watcher.Changed += watcher_Changed;
    watcher.EnableRaisingEvents = true;
}

public void watcher_Changed(object sender, FileSystemEventArgs e)
{
    // Jump twice
}

Why this event jump twice after my text file changed?

dur
  • 15,689
  • 25
  • 79
  • 125
user2214609
  • 4,713
  • 9
  • 34
  • 41
  • 3
    See [FileSystemWatcher - Pure Chaos (Part 1 of 2)](http://www.codeproject.com/Articles/58740/FileSystemWatcher-Pure-Chaos-Part-1-of-2) and [FileSystemWatcher - Pure Chaos (Part 2 of 2)](http://www.codeproject.com/Articles/58741/FileSystemWatcher-Pure-Chaos-Part-2-of-2) – Chris Sep 13 '13 at 23:05

1 Answers1

1

Here is the sample to avoid event raising.

public void OnChanged(object source, FileSystemEventArgs e)
{
    FileSystemWatcher watcher = null;
    try
    {
        watcher = (FileSystemWatcher)source;
        watcher.EnableRaisingEvents = false;
    }
    finally
    {
        if (watcher != null)
        {
            watcher.EnableRaisingEvents = true;
        }
    }
}
Marc
  • 3,905
  • 4
  • 21
  • 37
Hassan Nazeer
  • 357
  • 3
  • 5