【问题标题】:Understanding the difference between the following async-await calls了解以下 async-await 调用之间的区别
【发布时间】:2018-02-21 11:10:22
【问题描述】:
这些调用的行为是否相似?它们的行为是否与 async-await 的运行方式相同 - 可以或不能在相同的原始线程上下文上运行?还是第一个确保执行发生在不同的线程上下文中?
第一种方式-
Task task = SomeAsyncTask();
await task();
第二种方式——
await SomeAsyncTask();
【问题讨论】:
标签:
c#
async-await
threadpool
【解决方案1】:
如果Task task = SomeAsyncTask(); 和await task(); 之间有代码,您的任务将开始执行异步代码,并且您当前的线程将同时执行该代码。
否则,它们相等..
Task task = SomeAsyncTask();
//Codes executed on current thread while SomeAsyncTask is running
await task();