await vs Task.Wait - 死鎖? - await vs Task.Wait - Deadlock?

問題:

I don't quite understand the difference between Task.Wait and await .我不太明白Task.Waitawait之間的區別。

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?
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章