我們剛剛開始運行一個與FileSystemWatcher有關的Dispose()調用似乎掛起的奇怪問題。這是一段時間沒有任何問題的代碼,但我們只是升級到.NET3.5 SP1,所以我試圖找出是否有其他人看到過這種行爲。下面是創建FileSystemWatcher的代碼:FileSystemWatcher Dispose呼叫掛起
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的異化?
如果您使用FileSystemWatcher,則可能使用另一個線程; FileSystemWatcher線程可能被阻塞,試圖在UI線程上調用。 – 2008-09-30 16:41:03