C# Async - How does it work?

后端 未结 3 1034
走了就别回头了
走了就别回头了 2020-11-29 00:30

Microsoft announced the Visual Studio Async CTP today (October 28, 2010) that introduces the async and await keywords into C#/VB for asynchronous m

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-29 00:41

    As I understand it, what the async and await keywords do is that every time an async method employs the await keyword, the compiler will turn the remainder of the method into a continuation that is scheduled when the async operation is completed. That allows async methods to return to the caller immediately and resume work when the async part is done.

    According to the available papers there are a lot details to it, but unless I am mistaken, that is the gist of it.

    As I see it the purpose of the async methods is not to run a lot of code in parallel, but to chop up async methods into a number of small chunks, that can be called as needed. The key point is that the compiler will handle all the complex wiring of callbacks using tasks/continuations. This not only reduces complexity, but allows async method to be written more or less like traditional synchronous code.

提交回复
热议问题