如何重新创建"索引超出数组的范围"将项添加到词典?

Sta*_*wer 9 c# multithreading dictionary .net-3.5

我很容易在多线程.net 3.5应用程序上获得此错误

ERROR 26异常抛出.详细信息:'System.IndexOutOfRangeException:索引超出了数组的范围.

在System.Collections.Generic.Dictionary`2.Insert(TKey键,TValue值,布尔加法)

在System.Collections.Generic.Dictionary`2.Add(TKey键,TValue值)

我想我知道如何修复它(添加适当的锁)但我希望能够在我的本地环境中重新创建这个问题所以我会确定我修复了它并且我还能够添加单元测试为了它.

你知道重建这个的一致方法吗?

L.B*_*L.B 8

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

Task.Factory.StartNew(() => { while (true) dict["a"] = "a"; });
Task.Factory.StartNew(() => { while (true) dict.Remove("a"); });
Run Code Online (Sandbox Code Playgroud)