Accepted answer

"The dictionary passed in, is constantly increasing in size"

Do you mean that it's being modified while you're executing this code? That's a no-no. I suspect the ToList call is failing due to this. (After ToList() has executed, the list should be effectively separate from the dictionary.)

Basically Dictionary<TKey, TValue> doesn't support concurrent reading and writing. You might want to look at ConcurrentDictionary<,> which allows you to iterate over it while another thread is writing.

One suggestion to improve performance when it's all working: call side.ToString() once at the start of the method, instead of on every single loop iteration.

Related Articles