Keep your UI responsive. The BackgroundWorker component gives us an easy way to keep our UI responsive by moving a long-running process off of the UI thread. We'll explore this, plus other features: progress reporting, cancellation, and error handling. And we'll also see the BackgroundWorker component in conjunction with the MVVM pattern and the Task Parallel Library.
Keep your UI responsive. If we're not careful, a long-running process will cause our application to "lock up". The BackgroundWorker component gives us an easy-to-use solution by moving that long-running process off of our UI thread. We'll see how the BackgroundWorker component can keep our WinForms, WPF, or Silverlight UI responsive and also explore features such as progress reporting, cancellation, and error handling. After that, we'll see how easily the BackgroundWorker component fits in with an application using the Model-View-ViewModel (MVVM) pattern. And finally, we'll compare the BackgroundWorker component to the Task Parallel Library to make sure we're using the right tool for the job.
Jeremy Clark is an application developer, technical speaker, and Microsoft MVP with over 13 years of experience in all aspects of the development lifecycle. After hours, he puts together demos and technical articles for JeremyBytes.com, a website focusing on .NET technologies.
Progress, Cancellation, and Error Handling Now that we've seen the basic functionality of the BackgroundWorker component, it's time to dive into the details. The BackgroundWorker is a full featured component that supports progress reporting, cancellation, and error handling. Each of these requires communication between the background thread and the UI thread. The BackgroundWorker handles all of that communication for us.
BackgroundWorker and MVVM If you're working with XML based applications, it's hard to turn around without hearing someone refer to the model view view model pattern, or MVVM for short. Now I've been talking about the BackgroundWorker component for several years. In fact, it was one of my first topics when I started speaking publicly. One question that came up during those presentations was, how does the BackgroundWorker component fit in with MVVM? And the answer, quite well, actually. Let's see just how well.
BackgroundWorker vs. Task A very common question that comes up is whether the BackgroundWorker component is relevant in relation to modern technologies such as tasks in the Task Parallel Library. I've seen this question in several foreign posts. The best answer that I've come across is that comparing the BackgroundWorker and the Task Parallel Library is like comparing apples and oranges. Both tools serve a purpose. As we've seen, the BackgroundWorker is very specific and highly focused on a single purpose. In contrast, the Task Parallel Library supports a much broader use case and offers a wide variety of extremely powerful features. Now we're not going to take a deep dive into the Task Parallel Library, we're just going to see what it would take to replicate the functionality of the BackgroundWorker component using a task. For more information on the Task Parallel Library, be sure to check out courses available on Pluralsight on topics such as the TPL, asynchronous programming, and parallel programming.