Converting the APM pattern to tasks
In this recipe, we will see how to convert an old-fashioned APM API to a task. There are examples of different situations that could take place in the process of conversion.
Getting ready
To start this recipe, you will need Visual Studio 2015. There are no other prerequisites. The source code for this recipe can be found at BookSamples\Chapter4\Recipe4
.
How to do it...
To convert the APM pattern to tasks, carry out the following steps:
Start Visual Studio 2015. Create a new C# console application project.
In the
Program.cs
file, add the followingusing
directives:using System; using System.Threading.Tasks; using static System.Console; using static System.Threading.Thread;
Add the following code snippet below the
Main
method:delegate string AsynchronousTask(string threadName); delegate string IncompatibleAsynchronousTask(out int threadId); static void Callback(IAsyncResult ar) { WriteLine("Starting a callback..."); WriteLine($"State passed to a callbak: ...