This recipe shows you 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 2015. There are no other prerequisites. The source code for this recipe can be found at BookSamples\Chapter5\Recipe8
.
To design a custom awaitable type, perform 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.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks; using static System.Console; using static System.Threading.Thread;
Add the following code snippet below the
Main
method:static async Task AsynchronousProcessing() { var sync = new CustomAwaitable(true); string result = await sync; WriteLine(result); var async = new CustomAwaitable(false); result = await async; WriteLine(result); } class CustomAwaitable...