首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FileSystemWatcher Dispose调用挂起

FileSystemWatcher Dispose调用挂起
EN

Stack Overflow用户
提问于 2008-09-16 14:46:47
回答 3查看 5.6K关注 0票数 11

我们刚开始遇到一个奇怪的FileSystemWatcher问题,其中对Dispose()的调用似乎挂起了。这个代码已经工作了一段时间了,没有任何问题,但我们刚刚升级到.NET3.5 SP1,所以我想看看是否有人见过这种行为。下面是创建FileSystemWatcher的代码:

代码语言:javascript
复制
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();

它的使用方式是更新TreeNode对象的状态图像(略微调整以删除业务特定信息):

代码语言:javascript
复制
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;
         }
      }
   }
}

是我们遗漏了什么,还是这是.NET3.5 SP1的异常?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2008-09-16 14:52:27

只是一个想法..。这里有没有可能出现死锁问题?

您正在调用TreeView.Invoke,这是一个阻塞调用。如果文件系统更改恰好发生在您单击任何导致FileSystemWatcher.Dispose()调用的按钮时,您的FileWatcherFileChanged方法将在后台线程上被调用,并调用TreeView.Invoke,它将阻塞,直到您的form线程能够处理Invoke请求为止。但是,表单线程将调用FileSystemWatcher.Dispose(),它可能直到处理完所有挂起的更改请求后才会返回。

尝试将.Invoke更改为.BeginInvoke,看看这是否有帮助。这可能会帮助你找到正确的方向。

当然,这也可能是.NET 3.5SP1的问题。我只是根据你提供的代码在这里进行推测。

票数 9
EN

Stack Overflow用户

发布于 2008-09-30 16:42:07

斯科特,我们偶尔会看到.NET 2中的control.Invoke出现问题。尝试切换到control.BeginInvoke,看看这是否有帮助。

这样做将允许FileSystemWatcher线程立即返回。我怀疑您的问题可能是control.Invoke阻塞了,从而导致FileSystemWatcher在释放时冻结。

票数 2
EN

Stack Overflow用户

发布于 2008-09-17 22:33:36

我们也有这个问题。我们的应用程序运行在.Net 2.0上,但是是由VS2008 SP1编译的。我也安装了.NET 3.5 SP1。我也不知道为什么会发生这种情况,在我们这一端看起来不像是死锁问题,因为在这一点上没有其他线程在运行(在应用程序关闭期间)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73128

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档