小白终于弄懂了:c#从async/await到Task再到Thread
Posted hytvszz
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了小白终于弄懂了:c#从async/await到Task再到Thread相关的知识,希望对你有一定的参考价值。
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的主要内容,如果未能解决你的问题,请参考以下文章