Starting tasks inside a loop: how to pass values that can be changed inside the loop? [duplicate]
问题 This question already has answers here : Captured variable in a loop in C# (9 answers) Closed 12 days ago . I'm trying to use TPL inside a while loop and I need to pass to the task some values that then changes into the loop. For instance, here it is shown an example with an index that is incremented (necessarily after the line in which the task creation is requested): int index = 0; Task[] tasks; while(/*condition*/) { tasks[index] = Task.Factory.StartNew(() => DoJob(index)); index++; } But