Why does assigning a Task then awaiting it allow to run in parallel

前端 未结 1 1151
生来不讨喜
生来不讨喜 2021-01-22 01:26

I\'ve was playing around with async and I\'ve happened across some behaviour I\'ve not noticed before, if this is a duplicate please let me know, but my google-fu has failed me,

相关标签:
1条回答
  • 2021-01-22 02:03

    See comments in line

    async Task<Object> Bar()
    {
        var one =    Foo(3000); // <-- It can Start
        var two =    Foo(5000); // <-- It can Start
        var three =  Foo(3000); // <-- It can Start
    
        var x =
            new
            {
                One =   await one,  // <-- you are waiting for it to finish
                Two =   await two,  // <-- you are waiting for it to finish
                Three = await three,// <-- you are waiting for it to finish
            };      
            
        return x;
    }
    
    async Task<Object> Bar()
    {
        var one =   await Foo(3000); // <-- you are waiting for it to finish
        var two =   await Foo(5000); // <-- you are waiting for it to finish
        var three = await Foo(3000); // <-- you are waiting for it to finish
    
        var x =
            new
            {
                One =   one,
                Two =   two,
                Three = three,
            };
            
        return x;
    }
    
    0 讨论(0)
提交回复
热议问题