Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at
Simple threading using the BackgroundWorker class

A while back I was doing some work on my latest personal project at home (more on that later), and encountered a situation where I needed to run a long-running, resource intensive method, while at the same time updating a label control on a windows form. The process wasn't working very well, as the label wasn't getting updated properly, or in a timely manner, because the other method was using up all of the processor time allocated to the thread that they were both running in. So, I needed to figure out how to spin up the long-running method on a separate thread. I've never done any sort of multi-threading before, and didn't even really know where to start, so I just started browsing through all of the classes in the System.Threading namespace to see what my options were.

So, I started out with the Thread class. That actually seemed to do a pretty good job, and really did what I needed with one exception. It ran the task on a separate thread, and the main class was able to update the label control on the form effectively now, but I had a couple of things that I needed to do once the threaded task was complete. Unfortunately, the Thread class doesn't have any sort of mechanism for letting the class that started it know when it's done doing its thing. So, I needed to figure out how to do my Thread complete processing. As I was looking through some sample code in the MSDN library on using the Thread class, I noticed mention of the BackgroundWorker class. I looked up the documentation for this class, and as it turned out, that was exactly what I was looking for. From what I can tell (the documentation doesn't explicitly state this), it handles the process of spinning up a task on a new thread for you, and it also provides a number of events that you can subscribe to and handle, including the very one that I was looking for, RunWorkerCompleted, which fires once the threaded task has completed its processing. Perfect.

So, if you're working on an application in which you have need to do some asynchronous, threaded processing, take a look at the BackgroundWorker class. It might turn out to be just what you need.

Posted 09-26-2006 1:14 PM by Bob Yexley
Filed under: ,



Raimond Brookman wrote re: Simple threading using the BackgroundWorker class
on 09-26-2006 6:44 PM
One drawback of the backgroundworker class though: when it is processing you cannot submit a second task.. it wil throw an exception :-(
Marcin Hoppe wrote re: Simple threading using the BackgroundWorker class
on 09-27-2006 3:25 AM

Jon Skeet, C# MVP, has an awesome introduction to .NET threading: It doesn't cover BackgroundWorker but it gives a very practical introduction to .NET threading.

Bob Yexley wrote re: Simple threading using the BackgroundWorker class
on 09-27-2006 11:20 AM

Wow that looks like a fantastic article Marcin, thanks so much for the link. That could come in very handy for me, as I'm looking at the potential for needing to learn and do some more complex threading stuff in the near future. That should provide me with a good start for that. Thanks very much.

Add a Comment

Remember Me?

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Google Reader or Homepage Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of
Red-Gate Tools For SQL and .NET


SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
NHibernate Profiler
Balsamiq Mockups
JetBrains - ReSharper
Web Sequence Diagrams
Ducksboard<-- NEW Friend!


Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers


Community Server (Commercial Edition)