ThreadPool.QueueUserWorkItem with a lambda expression and anonymous method ThreadPool.QueueUserWorkItem with a lambda expression and anonymous method multithreading multithreading

ThreadPool.QueueUserWorkItem with a lambda expression and anonymous method


There is nothing wrong with this. The compiler is essentially doing automatically what you described as your alternative. It creates a class to hold the captured variables (test, s1 and s2) and passes a delegate instance to the lambda which is turned into a method on the anonymous class. In other words, if you went ahead with your alternative you would end up with soemthing very similar to what the compiler just generated for you.


For this particular example, no there is nothing wrong here. The state you've passed into the other thread is wholely contained and none of the types involve have any thread affinity issues.


It's a nice way of doing it. I don't see any disadvantages of using lambdas. It's simple and clean.