J H*_*unt 4 c# asynchronous task task-parallel-library async-await
我想创建一个异步函数,因为我想将结果返回到UI并且不想让它挂起,但无论如何它都是.
谁能告诉我为什么会挂?
public ActionResult Index()
{
return View(FunctionThreeAsync().Result);
}
private async Task<MyType> FunctionThreeAsync()
{
return await FunctionThree();
}
private Task<MyType> FunctionThree()
{
return Task.Run<MyType>(() => { /* code */ });
}
Run Code Online (Sandbox Code Playgroud)
这个:
return View(FunctionThreeAsync().Result);
Run Code Online (Sandbox Code Playgroud)
你的代码陷入僵局.你不应该阻止异步方法.相反,将您的方法标记为async,使其返回a Task<T>和await调用:
public async Task<ActionResult> DoStuffAsync()
{
return View(await FunctionThreeAsync());
}
Run Code Online (Sandbox Code Playgroud)
Async一路走来.如果您有一个异步方法,则需要异步等待,而不是同步阻塞.这意味着async遍布整个代码库.
| 归档时间: |
|
| 查看次数: |
506 次 |
| 最近记录: |