任务并行库替代BackgroundWorker?

发布于 2024-09-15 02:17:30 字数 185 浏览 4 评论 0原文

任务并行库是否有任何东西可以被视为对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

美人骨 2024-09-22 02:17:30

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

我的博客上有一个示例,展示了旧的 BackgroundWorker 处理方式和新的 Task 处理方式。我确实有一个用于需要报告进度的任务的小帮助程序类,因为我发现语法相当尴尬。该示例涵盖结果值、错误条件、取消和进度报告。

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.

小…红帽 2024-09-22 02:17:30

后台工作者仍然是实现这一目标的有效方法 - 如果您同时运行多个大型操作,那么并行扩展将值得考虑,如果它只是一个那么我会坚持使用后台工作者。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文