Why does Task.WaitAll() not block or cause a deadlock here?

前端 未结 3 1857
野的像风
野的像风 2020-12-09 05:41

In the example below two await calls are used. To gain performance, the sample gets converted Task.WaitAll() instead (not really any faster, but th

3条回答
  •  难免孤独
    2020-12-09 06:19

    Maybe this sample will demonstrate what might be happening. It's an iOS view loading. Try it with both the await call and without it (commented out below). Without any await in the function it will run synchronously and the UI will be blocked.

        public async override void ViewDidLoad()
        {
            base.ViewDidLoad ();
    
            var d1 = Task.Delay (10);
            var d2 = Task.Delay (10000);
    
            //await Task.Delay (10);
    
            Task.WaitAll (d1, d2);
    
            this.label.Text = "Tasks have ended - really!";
        }
    
        public override void ViewWillAppear(bool animated)
        {
            base.ViewWillAppear (animated);
            this.label.Text = "Tasks have ended - or have they?";
        }
    

提交回复
热议问题