How to avoid captured variables?
问题 I'm having a problem with foreach(var category in categories) { foreach(var word in words) { var waitCallback = new WaitCallback(state => { DoSomething(word, category); }); ThreadPool.QueueUserWorkItem(waitCallback); } } When the DoSomething gets executed, it receives the latest value for each captured variable instead of the value I desired. I can imagine a solution for this, but it imagine you guys can come up with better solutions 回答1: The canonical way to solve this is to copy the values