I've posted several articles about the new
awaitkeywords that are available in Visual Studio 2012, but I still see some people struggle with the concept. How do you make a task asynchronous? When and where do you use the
The answer is not simple because the need to process asynchronously depends on a variety of factors. There is overhead when you create a thread. It allocates memory and creates a new synchronization context. Having a thread communicate with other threads is even more expensive and adds complexity to your code. Fortunately, the teams at Microsoft introduced the Task Parallel Library to help simplify how you work with threads. I strongly recommend learning as much as you can about the
Taskobject if you wish to master asynchronous programming.
There are many cases where the use of asynchronous code is straightforward, and my intent with this article is to show a very simple example to help illustrate how to create an asynchronous task and then use the new keywords to interact with it. The example is a very trivial program that computes prime numbers between 2 and 999999. You can follow along and build this in about 5 minutes.
I liked the approach in this post in how it starts with code we might normally have written and show how it can be converted to run async.