I am trying to understand how I might be able to make better use of the .Net 4.5 TPL. Traditionally I used to manage the threads the old-school way, queueing and managing the threads directly.
I have created a silly program to explore the TPL, however, the code only seems to execute the last of the tasks added to my tasks list - I cannot determine why:
class Program {
static void Main(string[] args) {
var a = new A();
var b = new B();
var tasks = new List<Task<string>>();
for (int j = 33; j < 64; j++) {
tasks.Add(Task.Run(() => a.Go(j, 20)).ContinueWith((i) => b.Do(i)));
}
var finalTask = Task.Factory.ContinueWhenAll(tasks.ToArray(), r => {
foreach (var t in r)
Console.Write(t.Result);
});
finalTask.Wait();
Console.WriteLine("Finished.");
Console.ReadLine();
}
}
public class A {
public int Go(int chr, int loops) {
for (int i = 0; i < loops; i++) {
Thread.Sleep(10);
Console.Write((char)chr);
}
return loops;
}
}
public class B {
public string Do(Task<int> value) {
string s = "";
for (int i = 0; i < value.Result; i++) {
s = s + "a";
}
return s;
}
}
Any ideas why the other threads do not execute?