C# async/await Progress event on Task<> object

前端 未结 4 1919
一个人的身影
一个人的身影 2020-12-02 17:22

I\'m completely new to C# 5\'s new async/await keywords and I\'m interested in the best way to implement a progress event.

Now I\'d prefer

4条回答
  •  时光说笑
    2020-12-02 17:36

    When using a Task.Run lambda I have used an Invoke Action inside of this to update a ProgressBar control. This may not be the best way but it worked in a pinch without having to restructure anything.

       Invoke(new Action(() =>
    
                   {
                       LogProgress();
                   }));
    

    Which takes it to...

            private void LogProgress()
            {       
              progressBar1.Value = Convert.ToInt32((100 * (1.0 * LinesRead / TotalLinesToRead)));
            }
    

提交回复
热议问题