Is the new C# async feature implemented strictly i

2019-02-22 07:26发布

问题:

As a C# programmer whose interested in exploring "how things work", i am interested in understanding a bit more about the process that makes the new async feature work.

I have followed Eric Lippert's excellent article series on async: Async blog posts

I don't remember seeing anywhere any reference to the implementation of this feature (in a high level) except for the fact the the "compiler is doing most of the work" for us.

Is this feature strictly a compiler feature then? does the compiler rewrite the code in some manner and that's it? or are there other things like runtime support that make this happen?

回答1:

No, it's not entirely in the compiler. It relies on some new interfaces such as INotifyCompletion and some framework implementation support such as AsyncTaskMethodBuilder. I don't believe there are any CLR changes required though.

The compiler does a lot of work, building a state machine - it just refers to a few of those types within the state machine. Oh, and a lot of the Task-related types were significantly modified primarily for performance reasons.

I have a series of blog posts which were initially written against the CTP but only using vanilla .NET 4 and some classes I whipped up myself. They won't work against the production implementation (as things changed a bit over time) but they'll give you a general impression of what's going on behind the scenes.