FileSystemWatcher Changed event is raised twice

前端 未结 30 3840
死守一世寂寞
死守一世寂寞 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:28

    I've "fixed" that problem using the following strategy in my delegate:

    // fsw_ is the FileSystemWatcher instance used by my application.
    
    private void OnDirectoryChanged(...)
    {
       try
       {
          fsw_.EnableRaisingEvents = false;
    
          /* do my stuff once asynchronously */
       }
    
       finally
       {
          fsw_.EnableRaisingEvents = true;
       }
    }
    

提交回复
热议问题