在下面的代码中,当文件保存在记事本上时,即使我有Workaround,文件系统观察者事件也会被触发两次。有任何修复建议吗?
public void OnChanged(object sender, FileSystemEventArgs e)
{
try
{
if (MyGlobals.LastFired.Add(TimeSpan.FromSeconds(0)) < DateTime.UtcNow)
{
MyGlobals.LastFired = DateTime.UtcNow;
System.Threading.Thread.Sleep(2000);
Logging.Write_To_Log_File("Item change detected " + e.ChangeType + " " + e.FullPath + " " + e.Name, MethodBase.GetCurrentMethod().Name, "", "", "", "", "", "", 2);
MyGlobals.watchers.Clear();
foreach (FileSystemWatcher element in MyGlobals.watchers)
{
element.EnableRaisingEvents = false;
}
this.BeginInvoke((MethodInvoker)(() => CheckFilesAsync())); //
}
else
{
Logging.Write_To_Log_File("Change within the time limit", MethodBase.GetCurrentMethod().Name, "", "", "", "", "", "", 1);
}
}
catch (Exception ex)
{
// If exception happens, it will be returned here
}
finally
{
foreach (FileSystemWatcher element in MyGlobals.watchers)
{
element.EnableRaisingEvents = true;
}
}
}