This is part 3 in a series on using Task Parallel Library when writing server applications, especially ASP.NET MVC and ASP.NET Web API applications.
What ContinueWith Really Does
I promised that we had a bug in our previous code, and we did... sort of. Calling
ContinueWith without any special flags will cause your continuation to always run, regardless of the final state of the task. We wrote our continuation under the assumption that the
Task had completed successfully, which can lead to some very odd and hard to debug problems. Luckily, in our code, we ended up calling
Result on the
Task object, which turns around and throws an exception if the task had ended in a faulted or canceled state. But what if we'd had a
Task rather than a
Task<T>? Or what if we hadn't called
Result? In .NET 4, this is considered a fatal error condition, and when the task object got garbage collected, its finalizer would've thrown an exception that takes down your
AppDomain because you had an unobserved fault! Definitely not good.