Adding to a generic dictionary causes IndexOutOfRangeException

前端 未结 3 1276
孤城傲影
孤城傲影 2021-02-03 21:05

I\'m using a dictionary inside of some Task.

Logically I have set it up so that my Keys will never clash, though sometimes when I am adding to the dictionary I get this

相关标签:
3条回答
  • 2021-02-03 21:14

    Your issue is most likely synchronization. When a Dictionary is added to it sometimes needs to increase the size of the underlying structure (an array). If you are adding from multiple threads that may result in an IndexOutOfRangeException. You need to use locks etc. to make sure you are adding in a safe way.

    Alternatively you can use a ConcurrentDictionary which is a thread-safe collection.

    0 讨论(0)
  • 2021-02-03 21:21

    You should have looked to the documentation. That what it says:

    A Dictionary can support multiple readers concurrently, as long as the collection is not modified. Even so, enumerating through a collection is intrinsically not a thread-safe procedure. In the rare case where an enumeration contends with write accesses, the collection must be locked during the entire enumeration. To allow the collection to be accessed by multiple threads for reading and writing, you must implement your own synchronization. For a thread-safe alternative, see ConcurrentDictionary.

    0 讨论(0)
  • 2021-02-03 21:39

    So you might think Whatever! it will just break the one time - but nope:

    Important: Once the dictionary is broken it is broken!

    There goes three hours of sales (until IIS recycled on a schedule) because of a dictionary added for debugging purposes that wasn't ever even being read from.

    Note: This was running for 3.5 years before I hit this condition.

     private Dictionary<string, string> _debugLookup;
    
     _debugLookup[key] = virtualPath;
    

    This wasn't even a static dictionary - it was an MVC IViewLocationCache that was an instance method.

    0 讨论(0)
提交回复
热议问题