The BackgroundWorker class allows you to run an operation on a separate, dedicated thread. Time-consuming operations like downloads and database transactions can cause your user interface (UI) to seem as though it has stopped responding while they are running. When you want a responsive UI and you are faced with long delays associated with such operations, the BackgroundWorker class provides a convenient solution.
IntroI ran into a problem when I wanted to create an application to sync my savegames to Dropbox and I've found a powerful solution.
The problemI needed to use a Backgroundworker in my application so that I could execute code without my UI freezing. Now the problem was that the Backgroundworker only has 1 ReportUpdate function. My application has 3 progress bars that show the backup progress.
As you can see on the image, I'm using 3 progress bars to notify the user of the progress.
- Is the games progress. As you can see we currently have 2 games to process.
- Is the files process, this indicates how many files have to be uploaded/have been uploaded.
- Is the upload progress of the current file.
The solutionWe will use UserStates to solve this problem.
As we can see in the documentation, the userState is simply an Object. (ref. http://msdn.microsoft.com/en-us/library/a3zbdb1t.aspx)
So we can pass anything we like from the BackgroundWorker to our GUI.
So, let me show you some code. In my example I'll simply be passing a String as the userstate. (To keep things simple)
You can also pass an array of data or even a custom object that you've made yourself. Just keep in mind not to send too much overhead.
The BackgroundWorker codeWhen we've finished processing a game, I'll simply call the ReportProgress function and pass the string "games" as the UserState.
[csharp]worker.ReportProgress(Convert.ToInt32(((double)gamesProcessed / (double)numberOfGames) *100), "games");[/csharp]
I'll do the same for the files progress and the upload progress.
[csharp]worker.ReportProgress(Convert.ToInt32((double)currentFileIndex / (double)files.Count * 100), "files");[/csharp]
The upload progress is actually in a different function that recieves the progress as an argument, but that's not completely relevant.
[csharp]_worker.ReportProgress(e.PercentageProgress, "upload"); [/csharp]
The GUI codeSo now that we've reported the progress, we have to visualize it on the GUI.
_worker.ProgressChanged += delegate(object sender1, ProgressChangedEventArgs e1)
pbNumberofGames.Value = e1.ProgressPercentage;
pbProgress.Value = e1.ProgressPercentage;
else if (Equals(e1.UserState,"files"))
pbFiles.Value = e1.ProgressPercentage;
All we do here is update the right progress bar associated with the UserState. For example; If the UserState equals "games", we update the progress bar associated with the games progress. (the first progress bar, I've naded it pbNumberofGames)
And that's it. Now we can report an infinite amount of things from the Backgroundworker!
And you can create rich application when using a custom Object as the UserState.
~Michiel De Mey