This recipe shows how to design a very basic awaitable type that is compatible with the await
operator.
To step through this recipe, you will need Visual Studio 2012. There are no other prerequisites. The source code for this recipe can be found at BookSamples\Chapter5\Recipe8
.
For designing a custom awaitable type, 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.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks;
Add the following code snippet below the
Main
method:async static Task AsynchronousProcessing() { var sync = new CustomAwaitable(true); string result = await sync; Console.WriteLine(result); var async = new CustomAwaitable(false); result = await async; Console.WriteLine(result); } class CustomAwaitable { public CustomAwaitable(bool completeSynchronously...