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
  • 14,335
  • 22
  • 74
  • 113
user2214609
  • 4,383
  • 8
  • 30
  • 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,819
  • 4
  • 22
  • 34
Hassan Nazeer
  • 339
  • 3
  • 4