Since I was understanding the Task
in context of nested task, I really don't understand that- Why the 3rd print before 2nd print?
Even though, I have used Task.WaitAll(t)
, it print 3rd line before 2nd line.
Code:
public static void Main()
{
Task t = new Task(
() =>
{
Thread.Sleep(2000);
Console.WriteLine("1st print...");
});
t.ContinueWith(
x =>
{
Thread.Sleep(2000);
Console.WriteLine("2nd print...");
},
TaskContinuationOptions.OnlyOnRanToCompletion);
t.Start();
Task.WaitAll(t);
Console.WriteLine("3rd print...");
Console.Read();
}
Output:
You need to wait for the continuation also:
Task t2 = t.ContinueWith( /* .. */ );
Task.WaitAll(new [] { t, t2 } );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With