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 happen in the process of conversion.
Getting ready
To start this recipe, you will need Visual Studio 2012. There are no other prerequisites. The source code for this recipe can be found at BookSamples\Chapter4\Recipe4
.
How to do it...
For converting the APM pattern to tasks, perform the following steps:
Start Visual Studio 2012. Create a new C# Console Application project.
In the
Program.cs
file, add the followingusing
directives:using System; using System.Threading; using System.Threading.Tasks;
Add the following code snippet below the
Main
method:private delegate string AsynchronousTask(stringthreadName); private delegate string IncompatibleAsynchronousTask(outint threadId); private static void Callback(IAsyncResultar){ Console.WriteLine("Starting a callback..."); Console.WriteLine("State passed to a callback: {0}",ar.AsyncState...