C# FileSystemWatcher引发了两次事件
为什么在我的文本文件更改后此事件会跳两次?以下是避免引发事件的示例C# FileSystemWatcher引发了两次事件,c#,C#,为什么在我的文本文件更改后此事件会跳两次?以下是避免引发事件的示例 public void startWatch() { FileSystemWatcher watcher = new FileSystemWatcher(); watcher.Path = Path.GetDirectoryName(_file); watcher.Filter = Path.GetFileName(_file); watcher.NotifyFilter = NotifyFilt
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
}
看到和
public void OnChanged(object source, FileSystemEventArgs e)
{
FileSystemWatcher watcher = null;
try
{
watcher = (FileSystemWatcher)source;
watcher.EnableRaisingEvents = false;
}
finally
{
if (watcher != null)
{
watcher.EnableRaisingEvents = true;
}
}
}