Ris*_*ari 0 .net c# asynchronous async-await c#-5.0
以下哪一项是使用异步等待的最佳方法.
选项1:异步等待所有功能
public async Task A()
{
await B();
//some code
}
public async Task<bool> B()
{
var result= await C();
//some code
return result;
}
public Task<bool> C()
{
// not implemnted
}
Run Code Online (Sandbox Code Playgroud)
要么
选项2:异步等待仅在顶级功能中
public async Task A()
{
await B();
//some code
}
public async Task<bool> B()
{
var result= C().Result;
//some code
return result;
}
public Task<bool> C()
{
// not implemnted
}
Run Code Online (Sandbox Code Playgroud)
选项1是正确的方法,2不应该这样做.如果您在callstack中的任何位置使用async,则不应该在代码中调用.Result或.Wait()执行任务.如果你这样做,很可能你最终会使你的程序陷入僵局.
更新:旁注,如果函数B中的代码不依赖于启动函数的同一个线程(没有UI工作),那么"最好"的方法就是
public async Task A()
{
await B();
//some code that interacts with the UI
}
public async Task<bool> B()
{
var result= await C().ConfigureAwait(false);
//some code does does not interact with the UI
return result;
}
public Task<bool> C()
{
// not implemnted
}
Run Code Online (Sandbox Code Playgroud)
这使得系统可以使用任何可用的线程池线程,而不是在运行延续时等待同步上下文变为可用.