.net - .net哈希表插入失败。负载系数太高

标签 .net multithreading dictionary

我收到此错误:
哈希表插入失败。负载系数过高。
说明:执行当前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/

相关文章:

c# - 输出一个 ISO 8601 字符串

python - python中setInterval的等价物

python - 扭曲: `defer.execute` 和 `threads.deferToThread` 之间的区别

python - 如果嵌套字典中存在值,则查找键

java - 在包含 NULL 的 ArrayList 上使用 map {} 时出现 NPE

c# - VSIX:获取 DTE 对象

python - VBA 中的 HMAC-SHA1 URL 加密产生不正确的输出

c# - Entity Framework Core 在 QueryType(数据库 View )上使用 Include

c - 如何处理 n 个不同的 OpenMP 关键部分?

java - MapWritable 用新添加的键替换所有现有键 - Hadoop MapReduce 2.6.4