FileSystemWatcher.Changed 事件
中又修改文件时 会发生死循环
原因是:程序收到文件发生更改的通知后,马上又修改了文件,从而又使文件产生了更改的通知
static void watcher_Changed(object sender, FileSystemEventArgs e) { lock (obj) { File.AppendAllText(@"D:\test\t.txt", "test"); } FileSystemWatcher watcher = (FileSystemWatcher)sender; watcher.EnableRaisingEvents = false; System.Threading.Thread.Sleep(100); watcher.EnableRaisingEvents = true; }
这行代码似乎解决了问题:FileSystemWatcher watcher = (FileSystemWatcher)sender;
watcher.EnableRaisingEvents = false;
System.Threading.Thread.Sleep(100);
watcher.EnableRaisingEvents = true;
但有待验证