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?
BackgroundWorker is explicitly labeled as obsolete in .
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).
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With