FileSystemWatcher Dispose call hangs

We just started working with an odd problem with FileSystemWatcher, where the Dispose () call is called. This is code that worked without any problems for a while, but we just upgraded to .NET3.5 SP1, so I'm trying to find out if anyone has seen this behavior. Here is the code that creates the FileSystemWatcher:

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(); 

The way it is used is to update the state image of the TreeNode object (adjusted slightly to remove information about a particular business):

 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; } } } } 

Is there something we are missing or is this an anomaly from .NET3.5 SP1?

+7
c # winforms filesystemwatcher
source share
3 answers

Just a thought ... Does any case have a dead end problem here?

You call TreeView.Invoke, which is a blocking call. If the file system change happens just like you press any button that calls the FileSystemWatcher.Dispose () call, your FileWatcherFileChanged method will be called in the background thread and will call TreeView.Invoke, which will block until the form thread can process the request Invoke, However, your form stream will call FileSystemWatcher.Dispose (), which probably will not be returned until all pending change requests have been processed.

Try changing .Invoke to .BeginInvoke and see if that helps. It can help you in the right direction.

Of course, this could also be a .NET 3.5SP1 issue. I'm just thinking here based on the code you provided.

+7
source share
Scott, we sometimes saw problems with control.Invoke in .NET 2. Try switching to control.BeginInvoke and see if that helps.

This will allow the FileSystemWatcher stream to return immediately. I suspect that your problem is somehow related to the fact that control.Invoke is blocked, thereby causing the FileSystemWatcher to freeze after deletion.

+2
source share

We also have this problem. Our application runs on .Net 2.0, but VS 2008 SP1 is compiled. I also have .NET 3.5 SP1 installed. I have no idea why this happens and so, it does not look like a deadlock problem at our end, since at the moment no other thread is working (it is located during the application shutdown).

+1
source share

All Articles