ThreadPool.QueueUserWorkItem with a lambda expression and anonymous method

后端 未结 5 546
慢半拍i
慢半拍i 2020-12-29 03:46

Passing two parameters to a new thread on the threadpool can sometimes be complicated, but it appears that with lambda expressions and anonymous methods, I can do this:

5条回答
  •  醉酒成梦
    2020-12-29 04:34

    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.

提交回复
热议问题