You're closing over the loop variable, i
.
The bug actually exists in both programs.
Since the lambda defining the code to run in another thread closes over i
, and i
ends up being (potentially) changed by the main thread before the code actually runs. Take a copy of the loop variable inside of the loop and close over that.
Having said that, you can avoid the entire situation entirely using the TPL by not creating an external array that all of the tasks need to manually access. It's not a very TPL oriented way of doing things. Instead have each task independently create its result, and then aggregate the results:
var tasks = Enumerable.Range(0, 5)
.Select(_ => Task.Run(() => CreateScrapeFollower(cookie, proxy)));
var results = Task.WhenAll(tasks);