【问题标题】:Is it true that async-await is functionally equivalent to a task continuation?async-await 在功能上是否等同于任务继续?
【发布时间】:2016-12-15 19:33:15
【问题描述】:

我在看async-await的解释,上面写着

async Task<int> IndexWordsFromAsync(string url)
{
    string content = await httpClient.GetStringAsync(url);
    int wordCount = AddContentToIndex(content);
    return wordCount;
}

int AddContentToIndex(string content)
{
   ... 
}

等价于

Task<int> task = IndexWordsFromAsync(url);
var currentContext = SynchronizationContext.Current;
task.ContinueWith(delegate
{
    if(currentContext == null)
        RestOfMethod();
    else
        currentContext.Post(delegate { RegstOfMethod(); }, null);
}, TaskScheduler.Current);

有人可以确认这在多大程度上是正确的?或者你能把我链接到互联网上以这种方式描述async-await 的地方吗?

【问题讨论】:

    标签: c# .net asynchronous async-await task


    【解决方案1】:

    有人可以确认这在多大程度上是真的?

    你有理由怀疑吗?不用说你在哪里找到这个解释,很难批评它。

    那里有很多很多细节错误,但基本想法是合理的。 Async-await 是一种语法糖,用于构建由具有延续的任务组成的异步工作流。

    或者你能把我链接到互联网上以这种方式描述异步等待的地方吗?

    这不是推荐文章的网站;您可以进行自己的网络搜索。

    如果你想要我关于这个主题的文章,很容易找到它们:

    https://blogs.msdn.microsoft.com/ericlippert/tag/async/

    这些是从最近到最近的顺序,所以如果你想从头开始,请转到第二页的底部。

    您也可以考虑阅读 Jon Skeet、Stephen Cleary、Mads Torgersen 和 Stephen Toub 撰写的文章,这些文章也很容易找到。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-19
      • 2012-02-04
      • 2019-04-08
      • 2021-01-26
      • 1970-01-01
      • 2019-05-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多