title: How to make FileSystemWatcher more reliable
body: <p>I'm monitoring a folder on a network mapped drive using <code>FileSystemWatcher</code>. I know it can be janky and report repeated events and various other issues, but I'm also running into a problem of it reporting file modification before and my app not being able to open the file (as in, after receiving multiple <code>Changed</code> events, I try to open the file and get <code>FileNotFoundException</code>).</p>
  <p>My current protection:</p>
  <p>I keep a dictionary of per-filename timers and a list of things that were already processed:</p>
  <pre><code>    static private Dictionary&lt;string, Timer&gt; fileChangeTimeout = new Dictionary&lt;string, Timer&gt;();
      static private Mutex newFileHandlerLock = new Mutex();
      static private List&lt;(DateTime, string)&gt; recentlyHandled = new List&lt;(DateTime, string)&gt;();
  </code></pre>
  <p>Modifications are registered to fire after <code>waitTime</code> (3s) of no changes:</p>
  <pre><code>    private void ScheduleFileHandling(object sender, FileSystemEventArgs e){
          newFileHandlerLock.WaitOne();
  
          if(fileChangeTimeout.TryGetValue(e.FullPath, out var timer))
              timer.Change(waitTime, Timeout.Infinite);
          else
              fileChangeTimeout[e.FullPath] = new Timer(HandleFile, e.FullPath, waitTime, Timeout.Infinite);
  
          newFileHandlerLock.ReleaseMutex();
      }
  </code></pre>
  <p>No-more-writes delay timer finally runs out:</p>
  <pre><code>    private void HandleFile(object state)
      {
          newFileHandlerLock.WaitOne();
          // remove the timer
          // return if file in recentlyHandled with time difference &lt; 10s
          // add file to recentlyHandled
          newFileHandlerLock.ReleaseMutex();
  </code></pre>
  <p>This eliminated all the issues I've had with files either being empty or not fully written. But even after all that delay I sometimes run into &quot;file not found&quot; situations which don't make sense. (the file is in the folder when checked manually)</p>
  <p>Is there any way I can make this process more reliable, or do I have to resort to just retrying for a few seconds and hoping for the best?</p>