Update progress bar from async method

前端 未结 2 1028
时光取名叫无心
时光取名叫无心 2020-12-20 08:42

I am trying to understand better how can I update a windows forms progress bar from an async operation but I am getting some unexpected behavior from that.

Basically

2条回答
  •  失恋的感觉
    2020-12-20 08:56

    Your issue is happening because ExecuteMethodAsync(...) is not actually asynchronous.
    Add the following before the while loop to make it asynchronous

    await Task.Delay(1);
    

    or enclose some synchronous portion of code (e.g. the while loop) into a:

    await Task.Run(() => { ... });
    

    or (the best one), add the following at the beginning of the function:

    await Task.Yield(); // Make us async right away
    

提交回复
热议问题