mobile menu icon

C# Async/Await and Task Parallel Library

Published by Carlos Blé on 29/05/2015

C#, .Net

In order for a native desktop application to be responsive, I mean, not to freeze while sending a request over the network or processing a heavy CPU operation, these operations have to run in a separate thread.

.Net 4.0 introduced promises (a well-known concept for JavaScript developers), the Task object is a promise in C#. Later on, C# 5.0 introduced a nice syntactic helper to work with theses promises: async and await keywords.

Powerful features, comfortable to develop with but it’s important to know how they work behind scenes. Otherwise you could get in trouble with deadlocks and invalid operation exceptions.

Our teammate Modesto San Juan recommended the book “Async in C# 5.0” by Alex Davies, and I found it great. I’ve created a tiny sample app to summarize the things I’ve learned from the book:

Sample app: - Look at MainWindow.xaml.cs file.

The app is just a window with a bunch of buttons and a text field. Every button has an event handler in the MainWindow.xaml.cs that exercises each use case.

The method names in that file along with some comments, explain the most remarkable features for our team right now.

Most of the stuff in Alex’s book is very well summarized in this presentation by Alex Casquete & Lluis Franco:

Async Best Practices from Fimarge

Originally published in Carlos Blé's blog.

Volver a posts