Yielding Control

Now that you know how custom awaiters work, and how they interact with the compiler-generated code, let’s take a look at one more atypical awaiter that ships with the .NET Framework itself. On the Task class, you can find a static method called Yield, defined as follows:

public static YieldAwaitable Yield();

As the name of the return type implies, the result of calling Yield can be awaited. The effect of doing this is pausing the current asynchronous method, effectively yielding control and allowing for other work to be run before continuing the current method’s execution. Internally, this is simply accomplished by a custom awaiter whose IsCompleted property always returns false. Upon the rendezvous of the compiler-generated ...

Get C# 5.0 Unleashed now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.