I just started playing around with threading today and I ran into something that I dont understand.
public void Main()
{
int maxValue = 5;
for (int ID = 0; ID < maxValue; ID++)
{
temp(ID);
}
}
public void temp(int i)
{
MessageBox.Show(i.ToString());
}
As basic as it gets which works fine, but when I try to create a new thread for each, it only passes the maxValue. Please disregard how bad this is to do, I only wrote it this way as a simplistic example.
public void Main()
{
int maxValue = 5;
for (int ID = 0; ID < maxValue; ID++)
{
threads.Add(new Thread(() => temp(myString, rowID)));
threads[rowID].Start();
}
}
public void temp(string myString, int i)
{
string _myString = myString;
MessageBox.Show(i.ToString());
}
Given this, I have two questions: 1) Why doesnt a the method get called on a new thread passing the ID? 2) How should this correctly be coded?