Async wait block Main UI

后端 未结 3 802
长情又很酷
长情又很酷 2021-01-14 03:54

I am using the new async await features to upgrade from backgroundworker in C#. In the following code I am trying to replicate the execution of multiple tasks with ContinueW

3条回答
  •  长情又很酷
    2021-01-14 04:01

    When you use Task.Wait(), you are basically saying "wait here my task to complete". That's why you are blocking the thread. A good way to handle exception in tasks is using the Task.ContinueWith overload and pass OnlyOnFaulted as TaskContinuationOption which would look like:

    Task yourTask = new Task {...};
    yourTask.ContinueWith( t=> { /*handle expected exceptions*/ }, TaskContinuationOptions.OnlyOnFaulted );
    

提交回复
热议问题