I'm quite new in parallel programing
i'd like to do some work with tasks
every task is initiated with param to do some simple work with id
but it seems the params all mixed up..
i'm sure i'm missing some key element in thread safety
can you help me understand what i'm doing wrong ?
i don't need any return value from the tasks, i just need them to finish their work.
static void Main(string[] args)
{
int NumberOfTasks = 10;
Task[] tasks = new Task[NumberOfTasks];
for (int i = 0; i < NumberOfTasks; i++)
{
tasks[i] = Task.Factory.StartNew(() => DoSafeWork(i));
}
Task.WaitAll(tasks);
Console.WriteLine("Done !");
Console.ReadKey();
}
private static void DoSafeWork(int i)
{
Console.WriteLine("working on Task {0} ", i.ToString());
}
Current output (possible):
working on Task 3
working on Task 6
working on Task 10
working on Task 10
working on Task 10
working on Task 10
working on Task 10
working on Task 10
working on Task 10
working on Task 10
Done !