-1

I have a program where I have a list of requests being processed. e.g:

1st Request { Do Some operation }

2nd Request { Do Some operation } ...... ...

...Final Request { Do some operation }

I want the progress bar in my Windows form to fill accordingly as each of the requests are processed.

I know Multi-threading, async-await are a few options to update UI dynamically.

Which is the best way to go ahead so that there is minimal CPU load?

Nish
  • 183
  • 7
  • Have a look at the BackgroundWorker class: https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.backgroundworker?view=netframework-4.7.2 – cmos Jan 10 '19 at 10:49
  • 3
    The state-of-the-art is using async/await tasks and IProgress – Sir Rufo Jan 10 '19 at 10:50
  • Maybe you should need to update the bar with every request, don't update several times per second. Also you need to count the maximum count of operations. If you don't know the maximum number of operations, you'll better show a marquee bar. – ThorstenC Jan 10 '19 at 10:51
  • The best option, in my opinion is in an asyn way. [Take a Look](https://stackoverflow.com/questions/44959668/update-progress-bar-from-async-method) – andre Jan 10 '19 at 13:34

1 Answers1

1

Use async/await with or without tasks to execute the long-running operations and then use a Progress<int> to report the progress to the UI. There is an example on @Stephen Cleary's blog:

Task.Run vs BackgroundWorker, Round 5: Reporting Progress

mm8
  • 163,881
  • 10
  • 57
  • 88