Asynchronous Programming in the .NET Framework 4.5
When your user interface is unresponsive or your server does not scale, chances are you need your code to be more asynchronous. .NET Framework 4.5 introduces new language features in C# and Visual Basic to provide a new foundation for asynchrony in .NET programming. This new foundation makes asynchronous programming similar to - and about as straightforward as - synchronous programming.
When your user interface is unresponsive or your server does not scale, chances are your code needs to be more asynchronous. Microsoft .NET Framework 4.5 introduces new language features in C# and Visual Basic to provide a new foundation for asynchrony in .NET programming. This new foundation makes asynchronous programming very similar to synchronous programming.
In this Hands-On Lab you will learn how to implement asynchronous programming with the new features introduced by Microsoft .NET Framework 4.5.
Introduction to Asynchronous Operations
Asynchronous operations are operations that are initiated and continue running concurrently with the invoking code.
When conforming to the .NET Task-Based Asynchronous Pattern (TAP), asynchronous methods return a task, which represents the ongoing operation and enables you to wait for its eventual outcome.
Invoking and then waiting asynchronously for a task-returning method to complete has been simplified to a single-line instruction: “await myAsyncMethod();”. With this new asynchronous model, the control flow is completely unaltered. What is more, there are no callbacks because the compiler takes care of creating and signing them up.
Throughout this lab, you will become more familiar with the implementation of simple asynchronous logic to prevent blocking the UI when performing remote calls or intensive CPU processing.
You can find more references on .NET Asynchronous programming and the Task-Based Asynchronous Pattern at http://msdn.microsoft.com/async.
In this hands-on lab, you will learn how to:
Throughout the lab document, you will be instructed to insert code blocks. For your convenience, most of that code is provided as Visual Studio Code Snippets, which you can use from within Visual Studio to avoid having to add it manually.
To install the code snippets:
If you are not familiar with the Visual Studio Code Snippets, and want to learn how to use them, you can refer to the appendix from this document ‘Using Code Snippets’.
This hands-on lab includes the following exercises:
Each exercise is accompanied by a starting solution—located in the Begin folder of the exercise—that allows you to follow each exercise independently of the others. Please be aware that the code snippets that are added during an exercise are missing from these starting solutions and that they will not necessarily work until you complete the exercise.
Inside the source code for an exercise, you will also find an End folder containing a Visual Studio solution with the code that results from completing the steps in the corresponding exercise. You can use these solutions as guidance if you need additional help as you work through this hands-on lab.