小白终于弄懂了:c#从async/await到Task再到Thread
时间:2019-08-01 22:40:31
收藏:0
阅读:1374
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;
}
评论(0)