Reputation: 137
Here's the problem.
I'm trying to pass current variable value into a thread, is it possible without tricking it by create some text or objects?
In code it is something like this
List<Computer> students = new List<Computer>();
void main()
{
for(int i=0; i<students.Count; i++)
{
Thread thread = new Thread(new ThreadStart(() => Call(students[i])));
thread.Start();
}
}
void Call(Computer obj)
{
MessageBox.show(obj.Name);
Doconnect(obj.ip);
}
I'm trying to show every name with multiple thread so the application won't take along time to connect also...
Upvotes: 2
Views: 50
Reputation: 564433
Yes, it is possible. Since you're using a lambda, you need a local variable to avoid closure problems:
for(int i = 0; i < students.Count; i++)
{
var student = students[i];
Thread thread = new Thread(new ThreadStart(() => Call(student))); // use local
thread.Start();
}
That being said, it would be far better to just use the framework types, such as Parallel.ForEach:
Parallel.ForEach(students, s => Call(s));
Upvotes: 3