views:

113

answers:

2

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?

+2  A: 

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
And the Bgw will profit from the improved ThreadPool
Henk Holterman
A: 

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
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
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