Task parallel library replacement for BackgroundWorker?

被刻印的时光 ゝ 提交于 2019-11-26 08:53:10

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/3513432/task-parallel-library-replacement-for-backgroundworker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!