i start 4 threads in a loop. each thread gets a reference to an array element to write the result.
But on the line where i create each thread, i get a System.IndexOutOfRangeException. I'm amazed that the index "i" is going out of range.
here is an example:
void ThreadsStarter()
{
double[] data = new double[4];
for(int i = 0; i < 4; i++)
{
Thread my_thread = new Thread(() => Work(data[i]));
my_thread.Start();
}
}
void Work(double data)
{
}
Why this is happening?
reffrom your sample/title as unrelated to the question - feel free to edit/rollback. Noterefis generally hard to use/understand, especially when you start using more flexible collections likeList<double>instead ofArray. Try to avoidref- i.e. in this caseTuple.Create(data, i)could be an option (obviously created before lambda to avoid problem you have now).