As part of a TPL project, I'm chaining Tasks when they complete. The behavior of ContinueWith() is unintuitive to me. Consider the following snippet:
private void Execute()
{
var flow = Task.CompletedTask //Task.CompletedTask for the sake of context
.ContinueWith(async _ =>
{
var t1 = Task.Run(LongRunningTask1());
await t1;
})
.ContinueWith(async t =>
{
// STUFF
});
flow.Wait();
}
private Func<Task> LongRunningTask1()
{
return new Func<Task>(async () =>
{
Console.WriteLine("Task 1 Starting");
await Task.Delay(5000);
Console.WriteLine("Task 1 Complete");
});
}
This 'falls through' to // STUFF
without awaiting t1.
If I replace await t1
with t1.Wait()
it does behave as expected.
I'm really looking at understanding the difference in behavior here.