I have a List of objects and I'd like to loop over that list and start a new thread, passing in the current object.
I've written an example of what I thought should do this, but it's not working. Specifically, it seems like the threads are getting overwritten on each iteration. This doesn't really make sense to me though because I'm making a new Thread object each time.
This is the test code I wrote
class Program { static void Main(string[] args) { TestClass t = new TestClass(); t.ThreadingMethod(); } } class TestClass { public void ThreadingMethod() { var myList = new List<MyClass> { new MyClass("test1"), new MyClass("test2") }; foreach(MyClass myObj in myList) { Thread myThread = new Thread(() => this.MyMethod(myObj)); myThread.Start(); } } public void MyMethod(MyClass myObj) { Console.WriteLine(myObj.prop1); } } class MyClass { public string prop1 { get; set; } public MyClass(string input) { this.prop1 = input; } }
The output on my machine is
test2 test2
but I expected it to be
test1 test2
I tried changing the thread lines to
ThreadPool.QueueUserWorkItem(x => this.MyMethod(myObj));
but none of the threads started.
I think I just have a misunderstanding about how threads are supposed to work. Can someone point me in the right direction and tell me what I'm doing wrong?
Why C#'s foreach loop cannot change what it loops over. With a foreach loop we easily iterate over all elements in a collection. During each pass through the loop, the loop variable is set to an element from a collection.
ForEach loop works like a Parallel. For loop. The loop partitions the source collection and schedules the work on multiple threads based on the system environment. The more processors on the system, the faster the parallel method runs.
The execution of Parallel. Foreach is faster than normal ForEach.
How foreach loop works? The in keyword used along with foreach loop is used to iterate over the iterable-item . The in keyword selects an item from the iterable-item on each iteration and store it in the variable element . On first iteration, the first item of iterable-item is stored in element.
This is because you're closing over a variable in the wrong scope. The solution here is to use a temporary in your foreach loop:
foreach(MyClass myObj in myList) { MyClass tmp = myObj; // Make temporary Thread myThread = new Thread(() => this.MyMethod(tmp)); myThread.Start(); }
For details, I recommend reading Eric Lippert's post on this exact subject: Closing over the loop variable considered harmful
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With