<p>We just started running in to an odd problem with a FileSystemWatcher where the call to Dispose() appears to be hanging. This is code that has been working without any problems for a while but we just upgraded to .NET3.5 SP1 so I'm trying to find out if anyone else has seen this behavior. Here is the code that creates the FileSystemWatcher: </p>

<pre><code>if (this.fileWatcher == null)
{
   this.fileWatcher = new FileSystemWatcher();
}
this.fileWatcher.BeginInit();
this.fileWatcher.IncludeSubdirectories = true;
this.fileWatcher.Path = project.Directory;
this.fileWatcher.EnableRaisingEvents = true;
this.fileWatcher.NotifyFilter = NotifyFilters.Attributes;
this.fileWatcher.Changed += delegate(object s, FileSystemEventArgs args)
{
   FileWatcherFileChanged(args);
};
this.fileWatcher.EndInit();
</code></pre>

<p>The way this is being used is to update the state image of a TreeNode object (adjusted slightly to remove business specific information):</p>

<pre><code>private void FileWatcherFileChanged(FileSystemEventArgs args)
{
   if (this.TreeView != null)
   {
      if (this.TreeView.InvokeRequired)
      {
         FileWatcherFileChangedCallback d = new FileWatcherFileChangedCallback(FileWatcherFileChanged);
         this.TreeView.Invoke(d, new object[]
      {
         args
      });
      }
      else
      {
         switch (args.ChangeType)
         {
            case WatcherChangeTypes.Changed:
               if (String.CompareOrdinal(this.project.FullName, args.FullPath) == 0)
               {
                  this.StateImageKey = GetStateImageKey();
               }
               else
               {
                  projectItemTreeNode.StateImageKey = GetStateImageKey();
               }
               break;
         }
      }
   }
}
</code></pre>

<p>Is there something we're missing or is this an anomoly from .NET3.5 SP1?</p>

[{'answer_id': 73192, 'author': 'Jonathan Rupp', 'author_id': 12502, 'author_profile': 'https://Stackoverflow.com/users/12502', 'pm_score': 4, 'selected': True, 'text': "<p>Just a thought... Any chance there's a deadlock issue here?</p>\n\n<p>You're calling TreeView.Invoke, which is a blocking call.  If a filesystem change happens just as you're clicking whatever button causes the FileSystemWatcher.Dispose() call, your FileWatcherFileChanged method will get called on a background thread and call TreeView.Invoke, which will block until your form thread can process the Invoke request.  However, your form thread would be calling FileSystemWatcher.Dispose(), which probably doesn't return until all pending change requests are processed.</p>\n\n<p>Try changing the .Invoke to .BeginInvoke and see if that helps.  That may help point you in the right direction.</p>\n\n<p>Of course, it could also be a .NET 3.5SP1 issue.  I'm just speculating here based on the code you provided.</p>\n"}, {'answer_id': 88377, 'author': 'Jamie Penney', 'author_id': 68230, 'author_profile': 'https://Stackoverflow.com/users/68230', 'pm_score': 1, 'selected': False, 'text': "<p>We are also having this issue. Our application runs on .Net 2.0 but is compiled by VS 2008 SP1. I have .NET 3.5 SP1 installed as well. I've got no idea why this happens either, it doesn't look like a deadlock issue on our end as no other threads are running at this point (it is during application shutdown).</p>\n"}, {'answer_id': 153922, 'author': 'Judah Gabriel Himango', 'author_id': 536, 'author_profile': 'https://Stackoverflow.com/users/536', 'pm_score': 2, 'selected': False, 'text': "<p>Scott, we've occasionally seen issues with control.Invoke in .NET 2. Try switching to control.BeginInvoke and see if that helps. </p>\n\n<p>Doing that will allow the FileSystemWatcher thread to return immediately. I suspect your issue is somehow that the control.Invoke is blocking, thus causing the FileSystemWatcher to freeze upon dispose.</p>\n"}]
['https://Stackoverflow.com/questions/73128', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1559/']