【发布时间】:2012-08-22 15:52:36
【问题描述】:
我有一个 web.config 文件,我想解析它以防它发生变化。为此,我使用 FileSystemWatcher 和 IDictionary 集合。这是我的方法的代码:
public void ReadCurrentConfiguration()
{
var settings = System.Configuration.ConfigurationManager.AppSettings;
lock (_dictMonitor)
{
if (_dictionary.Count != 0)
_dictionary.Clear();
foreach (var elem in Enum.GetValues(typeof(ConfigurationParameter)).Cast<ConfigurationParameter>())
_dictionary.Add(elem.ToString(), settings[elem.ToString()]);
}
}
_dictMonitor为Object,即用于加锁; ConfigurationParameter 是枚举,用于定义键名。当 FileSystemWatcher 更改事件引发时调用此方法。 问题是程序执行在 _dictionary.Clear(); 或 lock(dictMonitor); 上停止,没有异常。调试时,手动调用我的方法 ReadCurrentConfiguration 执行良好 - 它正常清理字典并填充新值。但在事件引发的情况下,它会停止。此外,如果将执行传递给 clear() 方法,调试器将显示以下消息而不是 _dictionary 值:_dictionary Count = Unable to evaluate expression because the代码已优化或本机框架位于调用堆栈之上。 谁能指出为什么会出现此问题?
【问题讨论】:
-
是只有这一个线程挂起,还是您的应用中是否有其他线程同时挂起?
-
这个线程挂起后,我手动调用我的方法(这意味着不是在事件之后) - 它正常执行。这个调用在新线程中执行,所以主程序没有挂起。
-
顺便说一句,线程挂起后,新事件不会引发,直到我再次手动调用我的方法。
标签: .net multithreading dictionary web-config filesystemwatcher