Monday, April 1, 2013

Async and Parallel Programming in .NET 4

I spent some time recently on some of the latest features of .Net. One area I overlooked a while back was Async and parallel programming. I've done mostly web development and it hasn't always been beneficial to me. Lately I've expanded my talents, and this has come into play.

Essentially the Task Parallel Library (TPL) helps add parallelism to your application without having to worry too much about your thread pool or exceptions. You have to worry about these things, but the TPL has really helped out in the simplification of these areas. This is important because as technology advances, our applications need to be more snappy. For example, on the Windows Store applications, you may have multiple threads happening, but you cannot lose responsiveness to your application. Microsoft will reject your application if it does not respond.

Tasks are the fundamental building block. I think of tasks as function that will consume a large number of CPU cycles or require information from a third party. If we had a single threaded application, we'll be forced to wait for these functions to complete. However, we can create a task that will start up another thread and we can go on our doing other things.

As always with multi threading, there is additional complexity versus having a single threaded application. The TPL helps handle these issues. Each task will have a status, a result, and an exception that can be checked for processing. How variables are passed into tasks will impact your results. The order of execution can also be impacted because of multiple threads running at the same time. The TPL helps you handle it all.

I'm going to spend some time focusing on a few different areas of the TPL and show how they may be able to help you out in development.