All of the code in there is executed on a different thread and for that reason you are not allowed to touch the UI from it.

Instead, you bring data (from the UI or elsewhere) into the event by using the argument on the Run Worker Async() method, and the resulting data out of it by assigning it to the e. The Progress Changed and Run Worker Completed events, on the other hand, are executed on the same thread as the Background Worker is created on, which will usually be the main/UI thread and therefore you are allowed to update the UI from them.

Therefore, the only communication that can be performed between your running background task and the UI is through the Report Progress() method.

backgroundworker progresschanged not updating-41

It may appear to work but can cause unexpected issues.

Do not hesitate to contact us if you have other questions.

I can't argue with Jonathan's diagnosis, but I chose a cheap, punky way to handle it: I put a Thread.

By default, each time your application executes a piece of code, this code is run on the same thread as the application itself.

Each number between 0 and 10.000 gets tested to see if it's divisible with the number 7.

This is actually a piece of cake for today's fast computers, so to make it more time consuming and thereby easier to prove our point, I've added a one millisecond delay in each of the iterations.

This means that while this code is running, nothing else happens inside of your application, including the updating of your UI.

This is quite a surprise to people who are new to Windows programming, when they first do something that takes more than a second and realize that their application actually hangs while doing so.

This is where the Background Worker comes into play - it makes it simple, easy and fast to work with an extra thread in your application.