我们刚开始遇到一个奇怪的FileSystemWatcher问题,其中对Dispose()的调用似乎挂起了。这个代码已经工作了一段时间了,没有任何问题,但我们刚刚升级到.NET3.5 SP1,所以我想看看是否有人见过这种行为。下面是创建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对象的状态图像(略微调整以删除业务特定信息):
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的异常?
发布于 2008-09-16 14:52:27
只是一个想法..。这里有没有可能出现死锁问题?
您正在调用TreeView.Invoke,这是一个阻塞调用。如果文件系统更改恰好发生在您单击任何导致FileSystemWatcher.Dispose()调用的按钮时,您的FileWatcherFileChanged方法将在后台线程上被调用,并调用TreeView.Invoke,它将阻塞,直到您的form线程能够处理Invoke请求为止。但是,表单线程将调用FileSystemWatcher.Dispose(),它可能直到处理完所有挂起的更改请求后才会返回。
尝试将.Invoke更改为.BeginInvoke,看看这是否有帮助。这可能会帮助你找到正确的方向。
当然,这也可能是.NET 3.5SP1的问题。我只是根据你提供的代码在这里进行推测。
发布于 2008-09-30 16:42:07
斯科特,我们偶尔会看到.NET 2中的control.Invoke出现问题。尝试切换到control.BeginInvoke,看看这是否有帮助。
这样做将允许FileSystemWatcher线程立即返回。我怀疑您的问题可能是control.Invoke阻塞了,从而导致FileSystemWatcher在释放时冻结。
发布于 2008-09-17 22:33:36
我们也有这个问题。我们的应用程序运行在.Net 2.0上,但是是由VS2008 SP1编译的。我也安装了.NET 3.5 SP1。我也不知道为什么会发生这种情况,在我们这一端看起来不像是死锁问题,因为在这一点上没有其他线程在运行(在应用程序关闭期间)。
https://stackoverflow.com/questions/73128
复制相似问题