c# - 如何重新创建 "Index was outside the bounds of the array"添加项目到字典?

标签 c# multithreading dictionary .net-3.5

我一直在多线程 .net 3.5 应用程序上遇到此错误

ERROR 26 Exception thrown. Details: 'System.IndexOutOfRangeException: Index was outside the bounds of the array.

at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)

at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)

我想我知道如何修复它(在适当的地方添加锁),但我希望能够在我的本地环境中重现这个问题,所以我将确保我修复了它并且我能够添加对其进行单元测试。

你知道任何一致的方法来重新创建它吗?

最佳答案

Dictionary<string, string> dict = new Dictionary<string, string>();

Task.Factory.StartNew(() => { while (true) dict["a"] = "a"; });
Task.Factory.StartNew(() => { while (true) dict.Remove("a"); });

关于c# - 如何重新创建 "Index was outside the bounds of the array"添加项目到字典?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12748275/

相关文章:

C# excel单元格多色文本

c# - 每 10% 输入一个代码分支,直到 100%

java - ArrayList 可以在多线程环境中用于只读目的吗?

python - Nosetests assert_equal 显然不等于 python ==

c# - 从列表 2 中过滤列表 1

c# - 在任何子方法之前(或之后)调用父方法

objective-c - 为什么我的 Cocoa 应用程序中的某些行需要永远运行?

c - TFTP 中的超时和重新传输问题

python - 将 .txt 文件处理成字典 (Python v2.7)

ios - Swift:如何将字典数组添加到数组?