FileSystemWatcher Changed event is raised twice

前端 未结 30 3870
死守一世寂寞
死守一世寂寞 2020-11-22 06:01

I have an application where I am looking for a text file and if there are any changes made to the file I am using the OnChanged eventhandler to handle the event

30条回答
  •  青春惊慌失措
    2020-11-22 06:35

    One possible 'hack' would be to throttle the events using Reactive Extensions for example:

    var watcher = new FileSystemWatcher("./");
    
    Observable.FromEventPattern(watcher, "Changed")
                .Throttle(new TimeSpan(500000))
                .Subscribe(HandleChangeEvent);
    
    watcher.EnableRaisingEvents = true;
    

    In this case I'm throttling to 50ms, on my system that was enough, but higher values should be safer. (And like I said, it's still a 'hack').

提交回复
热议问题