So, this is my code:
I would like to know the reason why, my progress bar keeps updating even after the ‘foreach’ loop has finished.

Basically, I’ve got a long .json file (200k non-prettified lines) which is the result of some serialized custom objects. Now I want to have a visible progress bar while my loop is reading through that file, but all I get is a stuck UI thread waiting for the progress bar to finish, even though the ‘Task t’ has moved on with its continuation method…
Obviously I’ve got something wrong here. Any thoughs?


You are using `BeginInvoke` in the foreach loop. That is an asynchronous method and is queue all those actions on the UI thread. And apparently piling up there.
You can use Invoke instead for the call to wait for the UI to be updated. Or put an invoke after the loop so it waits for all actions to run on the UI thread.
But seriously, if updating the progress bar is more computationally expensive that the actual work, you should really consider just updating the progress bar less often.
Okey, progressBar1.Invoke() did the trick and I get the functionality that I want, but now there’s another question; is updating the progressbar really that expensive? The loop itself needs something like 3 seconds to finish, but If I have the progress bar updated, then the whole thing takes up to 20 !!!
C# devs
null reference exceptions

source

Categories: .NetCodingCsharp