Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Task parallel library replacement for BackgroundWorker?

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?

like image 219
Keith G Avatar asked Aug 18 '10 14:08

Keith G


People also ask

Is BackgroundWorker obsolete?

BackgroundWorker is explicitly labeled as obsolete in .

What is the difference between BackgroundWorker and thread?

BackgroundWorker has already implemented functionality of reporting progress, completion and cancellation - so you don't need to implement it by yourself. Usage of Thread gives you more control over the async process execution (e.g. thread priority or choosing beetween foreground/background thread type).

What is TPL Task Parallel Library and how it differs from threads?

Threading. Tasks namespaces. The purpose of the TPL is to make developers more productive by simplifying the process of adding parallelism and concurrency to applications. The TPL dynamically scales the degree of concurrency to use all the available processors most efficiently.

Does TPL use ThreadPool?

TPL combines all the good stuff the Thread and ThreadPool classes have and it is a modern way of running parallel tasks in . NET apps. Therefore, it provides notification capabilities, parallel task control (i.e. continue, wait, etc.) as well as automated management and tuning of threads.


1 Answers

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.

like image 173
Stephen Cleary Avatar answered Oct 04 '22 22:10

Stephen Cleary