This is part 4 in a series on using Task Parallel Library when writing server applications, especially ASP.NET MVC and ASP.NET Web API applications.
- Introduction
- SynchronizationContext
- ContinueWith
- TaskHelpers
Simple Task Helpers
I know we left off in part 3 with a problem, but we're not going to solve it yet, because we still need to build some foundation helper classes that will make it easier to do that. In particular, there are some good practices we can codify around the creation and conversion of tasks that led us to write the TaskHelpers class that we use today in ASP.NET Web Stack.
Continue reading "Task Parallel Library and Servers, Part 4: TaskHelpers" »
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.
- Introduction
- SynchronizationContext
- ContinueWith
- TaskHelpers
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.
Continue reading "Task Parallel Library and Servers, Part 3: ContinueWith" »
This is part 2 in a series on using Task Parallel Library when writing server applications, especially ASP.NET MVC and ASP.NET Web API applications.
- Introduction
- SynchronizationContext
- ContinueWith
- TaskHelpers
Introduction to SynchronizationContext
An important part of the work in properly handling tasks on the server is supporting the synchronization context. When you’re using .NET 4.5, then the await
keyword automatically does this for you. When you’re consuming Task
objects on .NET 4, though, getting yourself back onto the right synchronization context is critical; otherwise, you may cause errors in your application when trying to access things which touch the HttpContext
in ASP.NET.
Continue reading "Task Parallel Library and Servers, Part 2: SynchronizationContext" »
This is part 1 in a series on using Task Parallel Library when writing server applications, especially ASP.NET MVC and ASP.NET Web API applications.
- Introduction
- SynchronizationContext
- ContinueWith
- TaskHelpers
Okay, let’s just get this out of the way:
Asynchronous (multi-threaded) programming is not easy.
This warning really has nothing to do with .NET in particular, because it can be quite challenging to do correctly on any framework, but here’s the good news:
Asynchronous programming with .NET 4 is a little easier.
Asynchronous programming with .NET 4.5 is a lot easier.
Continue reading "Task Parallel Library and Servers, Part 1: Introduction" »
Recent Comments