84

Does the task parallel library have anything that would be considered a replacement or improvement over the BackgroundWorker class?

I have a WinForms application with a wizard-style UI, and it does some long-running tasks. I want to be able to have a responsive UI with the standard progress bar and ability to cancel the operation. I've done this before with BackgroundWorker, but I'm wondering if there are some TPL patterns that can be used instead?

Keith G
  • 4,580
  • 5
  • 38
  • 48
  • 1
    Also see http://stackoverflow.com/questions/4054263/how-does-c-sharp-5-0s-async-await-feature-differ-from-the-tpl and http://stackoverflow.com/questions/12414601/async-await-vs-backgroundworker – nawfal May 14 '13 at 06:07

2 Answers2

92

The Task class is an improvement over the BackgroundWorker; it naturally supports nesting (parent/child tasks), uses the new cancellation API, task continuations, etc.

I have an example on my blog, showing the old BackgroundWorker way of doing things and the new Task way of doing things. I do have a small helper class for tasks that need to report progress, because I find the syntax rather awkward. The example covers result values, error conditions, cancellation, and progress reporting.

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
  • Tough deciding between this and Hatch's answer since his is technically a correct answer. However, your blog shows how to use the new Task class, and that's really what I was looking for - an evolution from BackgroundWorker. I'm using your example as a basis for code in my application. – Keith G Aug 25 '10 at 17:54
  • 2
    A few days ago I wrote up a [comparison of various background processing techniques](http://nitoprograms.blogspot.com/2010/08/various-implementations-of-asynchronous.html). `BackgroundWorker` has easier progress reporting, while `Task` allows nesting. Of the two, I prefer `Task` (it's *much* easier to clean up progress reporting than to permit nesting). They're both light years ahead of other common solutions, though. I cringe when I hear of people using `Thread` or `ThreadPool.QueueUserWorkItem`. They are the absolute hardest to use correctly for background tasks. – Stephen Cleary Aug 26 '10 at 03:25
  • Easier progress reporting? Only if all you want to report a percentage. Reporting anything else requires begininvoke or SynchronizationContext.Post ugliness – Panagiotis Kanavos Apr 19 '13 at 11:24
  • 5
    @PanagiotisKanavos: You can pass any arbitrary object as `userState`. However, in the 2.5 years since my last comment, `Task` has been supplemented with `IProgress` and `Progress`, which are cleaner and easier than `BackgroundWorker`'s progress. In modern code, there is no reason to use `BackgroundWorker` anymore at all. – Stephen Cleary Apr 19 '13 at 12:09
  • Yeah, I just noticed. Somehow, this post came topmost when I clicked on the TPL tag to find new questions! – Panagiotis Kanavos Apr 19 '13 at 12:11
23

Background worker is still a valid way of achieving this - if you are running multiple large operations concurrently then the parallel extensions would be worth considering, if its just the one then I would stick with the backgroundworker.

Hatch
  • 296
  • 1
  • 4