码迷,mamicode.com
首页 > Windows程序 > 详细

小白终于弄懂了:c#从async/await到Task再到Thread

时间:2019-08-01 22:40:31      阅读:1374      评论:0      收藏:0      [点我收藏+]

标签:sum   wait   public   span   work   OLE   one   int   ret   

1. 怎么解决async/await的无限嵌套

public async Task MyMethodAsync()
{
    Task<int> longRunningTask = LongRunningOperationAsync();
    // independent work which doesn‘t need the result of LongRunningOperationAsync can be done here

    //and now we call await on the task 
    int result = await longRunningTask;
    //use the result 
    Console.WriteLine(result);
}

public async Task<int> LongRunningOperationAsync() // assume we return an int from this long running operation 
{
    await Task.Delay(1000); // 1 second delay
    return 1;
}

小白终于弄懂了:c#从async/await到Task再到Thread

标签:sum   wait   public   span   work   OLE   one   int   ret   

原文地址:https://www.cnblogs.com/hytvszz/p/11285792.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!