任务并行库替换 Background Worker? ?

任务并行库是否有任何可以被认为是对 BackoundWorker 类的替代或改进的内容?

我有一个具有向导样式 UI 的 WinForms 应用程序,它执行一些长时间运行的任务。我希望能够有一个具有标准进度条的响应 UI 和取消操作的能力。我以前用 BackoundWorker 做过这个,但是我想知道是否有一些 TPL 模式可以替代?

37074 次浏览

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.

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.