我收到此错误:
哈希表插入失败。负载系数过高。
说明:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪,以获取有关错误及其在代码中起源的更多信息。
异常详细信息:System.InvalidOperationException:哈希表插入失败。负载系数过高。
在我的代码中,如果我没有在字典中找到键,则添加它。经过研究后,我认为上面的错误是因为我试图两次添加相同的 key 。
static Dictionary<string, string> settings =
new Dictionary<string, string>();
if ((!settings.ContainsKey(Key)) || (settings[Key] == null))
settings.Add(Key, AltValue);//Changes by Reliance Consulting
有没有更安全的方法可以做到这一点?
谢谢!
最佳答案
您没有说什么框架,但我假设是.NET:
This error is almost always caused by multiple threads modifying the Hashtable at the same time. The fix is to insert locks before modifying the Hashtable, since Hashtable isn't multiple writer threadsafe.
对于.NET 2.0 ASP.NET环境,有一个修补程序可以解决您的问题:
FIX: A System.InvalidOperationException exception occurs when you run a Web application that is based in the .NET Framework 2.0 SP2 or in the .NET Framework 3.5 SP1
关于.net - .net哈希表插入失败。负载系数太高,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2151219/