Quote:
Originally Posted by XV50
Yeah, removing Code from the ImportFactory so he can't parse anything.
Stop posting useless **** comments just to farm some posts.
|
I haven’t said to remove this line. I suggested to remove code he doesn’t understand. That is maybe most of the project but still. It is a good way to learn part by part.
But as you seems to ask for the real answer to the question (that he will never understand) there it is. List in c# use array under the hood. Arrays are fixed size. When adding to the list c# vm is allocating new memory if needed to your array. In case of parallel it add based on previous value. (Size+1) unfortunately as it’s doing multiple thing it’s trying size + 1 multiple time. Doing it at the same time result in the value being wrong as the difference between the read and wrote value make that the original value changed.
Thread 1: what is the amount of slot => 0
Thread 2: what is the amount of slot => 0
Thread 1 : add 1 => 0+1 => 1
Thread 2 : add 1 => 0+1 => 1 Thread 1 already used this so it crash.
So how to fix it ? You have to use a threadsafe collection type to avoid this kind of error. Like a concurrent dictionary.
Ps this have been simplified that’s not totally what is happening but would be useless to explain in detail