using System.Collections.Generic;
using System.Threading.Tasks;
public static void Main()
Console.WriteLine("Situation 1, for loop runs faster than new tasks, race condition");
var tasks = new List<Task>();
List<int> nums = new List<int>() {1,2,3,4,5};
tasks.Add(new TaskFactory().StartNew(() =>
Console.WriteLine("current task: {0}", i);
Task.WaitAll(tasks.ToArray());
Console.WriteLine("Situation 2, tasks run faster, everything looks OK");
tasks.Add(new TaskFactory().StartNew(() =>
Console.WriteLine("current task: {0}", i);
Console.WriteLine("Fix, even through for loop runs fast, that's OK");
tasks.Add(new TaskFactory().StartNew(() =>
Console.WriteLine("current task: {0}", tempI);
Task.WaitAll(tasks.ToArray());