問題:
I don't quite understand the difference between Task.Wait
and await
.我不太明白Task.Wait
和await
之間的區別。
I have something similar to the following functions in a ASP.NET WebAPI service:我在 ASP.NET WebAPI 服務中有類似於以下功能的東西:
public class TestController : ApiController
{
public static async Task<string> Foo()
{
await Task.Delay(1).ConfigureAwait(false);
return "";
}
public async static Task<string> Bar()
{
return await Foo();
}
public async static Task<string> Ros()
{
return await Bar();
}
// GET api/test
public IEnumerable<string> Get()
{
Task.WaitAll(Enumerable.Range(0, 10).Select(x => Ros()).ToArray());
return new string[] { "value1", "value2" }; // This will never execute
}
}
Where Get
will deadlock.哪裏Get
會死機。
What could cause this?什麼可能導致這種情況? Why doesn't this cause a problem when I use a blocking wait rather than await Task.Delay
?當我使用阻塞等待而不是await Task.Delay
時,爲什麼這不會導致問題?
解決方案:
參考一: https://stackoom.com/question/t8Rv參考二: await vs Task.Wait - Deadlock?