BackgroundWorker 的任务并行库替换? [英] Task parallel library replacement for BackgroundWorker?

查看:21
本文介绍了BackgroundWorker 的任务并行库替换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任务并行库是否有任何可以被视为对 BackgroundWorker 类的替代或改进的东西?

Does the task parallel library have anything that would be considered a replacement or improvement over the BackgroundWorker class?

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

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?

推荐答案

Task 类是对 BackgroundWorker 的改进;它自然支持嵌套(父/子任务),使用新的取消 API,任务延续等.

The Task class is an improvement over the BackgroundWorker; it naturally supports nesting (parent/child tasks), uses the new cancellation API, task continuations, etc.

我的博客上有一个例子,显示旧的 BackgroundWorker 做事方式和新的 Task 做事方式.我确实有一个用于需要报告进度的任务的小助手类,因为我发现语法相当笨拙.该示例涵盖了结果值、错误条件、取消和进度报告.

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.

这篇关于BackgroundWorker 的任务并行库替换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆