BackgroundWorker – loading in the background – example C#


Thanks to this class, we can open the window(form) immediately and eg. all data can be loaded in the background. This is usefull becouse basic Windows application runs on a single thread and when we’ve got big list to load on this form. We have to wait until this list and all the others components are loaded then we can display this form.This is sometimes a nuisance. With help coming here BackgroundWorker class – we can upload data asynchronously. I’ll show you how it’s work on example.


I’ll write it on WinForms but it’s looks similar on WPF. We need to hook up a few events.

To the event DoWork we hook up a function that will perform in the background. To ProgressChanged we hook up the code that will show progress and to RunWorkerCompleted a method that informs us of a successful or not successful completion. Propertion WorkerSupportsCancellation allow for the process to be cancelled.

Event DoWork in the loop we sleep the thread for 0.5 seconds and every time we can cancel our operation.

ProgressChanged informs us of progress.

And event it informs us that the data upload has finished positive or not.

Also add a button to cancel the action.

We run our BackgroundWorker class by calling the method:


With this simple way we don’t block main UI thread.


Link to project.

Leave a Reply

Your email address will not be published. Required fields are marked *