How does C# 5.0's async-await feature differ from the TPL?

后端 未结 7 1677
孤城傲影
孤城傲影 2020-12-07 10:48

I don\'t see the different between C#\'s (and VB\'s) new async features, and .NET 4.0\'s Task Parallel Library. Take, for example, Eric Lippert\'s code from here:

         


        
7条回答
  •  被撕碎了的回忆
    2020-12-07 10:52

    The problem here is that the signature of ArchiveDocuments is misleading. It has an explicit return of void but really the return is Task. To me void implies synchronous as there is no way to "wait" for it to finish. Consider the alternate signature of the function.

    async Task ArchiveDocuments(List urls) { 
      ...
    }
    

    To me when it's written this way the difference is much more obvious. The ArchiveDocuments function is not one that completes synchronously but will finish later.

提交回复
热议问题