Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# FileSystemWatcher Dispose调用挂起_C#_.net_Winforms_Filesystemwatcher - Fatal编程技术网

C# FileSystemWatcher Dispose调用挂起

C# FileSystemWatcher Dispose调用挂起,c#,.net,winforms,filesystemwatcher,C#,.net,Winforms,Filesystemwatcher,我们刚开始遇到一个奇怪的问题,FileSystemWatcher对Dispose()的调用似乎挂起了。这段代码已经运行了一段时间,没有任何问题,但我们刚刚升级到.NET3.5sp1,所以我试图找出是否有其他人见过这种行为。以下是创建FileSystemWatcher的代码: if (this.fileWatcher == null) { this.fileWatcher = new FileSystemWatcher(); } this.fileWatcher.BeginInit(); t

我们刚开始遇到一个奇怪的问题,FileSystemWatcher对Dispose()的调用似乎挂起了。这段代码已经运行了一段时间,没有任何问题,但我们刚刚升级到.NET3.5sp1,所以我试图找出是否有其他人见过这种行为。以下是创建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();
使用这种方法是更新TreeNode对象的状态映像(稍微调整以删除特定于业务的信息):


我们有没有遗漏什么,或者这是.NET3.5 SP1的反常现象?

只是一个想法。。。这里有没有可能出现僵局的问题

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

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


当然,它也可能是.NET3.5SP1的问题。我只是根据您提供的代码进行推测。

我们也遇到了这个问题。我们的应用程序运行在.NET2.0上,但由VS2008SP1编译。我也安装了.NET3.5SP1。我也不知道为什么会发生这种情况,这看起来不像是死锁问题,因为此时没有其他线程在运行(这是在应用程序关闭期间)。

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


这样做将允许FileSystemWatcher线程立即返回。我怀疑您的问题在于控件.Invoke被阻塞,从而导致FileSystemWatcher在dispose时冻结。

如果您使用的是FileSystemWatcher,则可能正在使用另一个线程;试图在UI线程上调用时,FileSystemWatcher线程可能被阻止。这似乎是正确的解决方案。我把答案授予乔纳森,因为他是第一个回答的人。
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;
         }
      }
   }
}