任务并行库替代BackgroundWorker?
任务并行库是否有任何东西可以被视为对 BackgroundWorker 类的替代或改进?
我有一个带有向导式 UI 的 WinForms 应用程序,它执行一些长时间运行的任务。我希望能够拥有一个带有标准进度条的响应式 UI,并且能够取消操作。我以前用BackgroundWorker 做过这个,但我想知道是否有一些TPL 模式可以用来代替?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Task
类是对BackgroundWorker
的改进;它自然支持嵌套(父/子任务),使用新的取消 API、任务延续等。我的博客上有一个示例,展示了旧的
BackgroundWorker
处理方式和新的Task
处理方式。我确实有一个用于需要报告进度的任务的小帮助程序类,因为我发现语法相当尴尬。该示例涵盖结果值、错误条件、取消和进度报告。The
Task
class is an improvement over theBackgroundWorker
; 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 newTask
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.后台工作者仍然是实现这一目标的有效方法 - 如果您同时运行多个大型操作,那么并行扩展将值得考虑,如果它只是一个那么我会坚持使用后台工作者。
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.