委托与后台工作人员的区别?

发布于 2024-10-08 22:14:41 字数 116 浏览 4 评论 0原文

谁能解释一下Delegates和BackgroundWorker之间的区别?在这种情况下,BackGroungWorker比Delegate更有效率?既然我们有异步委托,为什么需要使用BackGroungWorker。

Can anyone explain the difference between Delegates and BackgroundWorker?In which case Backgroundworker is more efficient than Delegate?Since we are having asynchronous delegate what is the need to use BackGroungWorker.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

孤独岁月 2024-10-15 22:14:41

BackgroundWorker

BackgroundWorker 类允许您在单独的专用线程上运行操作。

委托

委托是一种定义方法签名的类型。 ...委托用于将方法作为参数传递给其他方法。


使用哪一个的问题与效率无关。

BackgroundWorker 是一个简化线程使用的包装器,您也可以使用异步委托,但正确管理它们要困难得多。或者,来自 MSDN:

当您需要响应式 UI 并且面临与此类操作相关的长时间延迟时,BackgroundWorker 类提供了一个方便的解决方案。

BackgroundWorker:

The BackgroundWorker class allows you to run an operation on a separate, dedicated thread.

Delegate:

A delegate is a type that defines a method signature. ... Delegates are used to pass methods as arguments to other methods.


The question of which one to use has nothing to do with efficiency.

BackgroundWorker is a wrapper that simplifies working with threads, you can use async delegates just as well, but managing them correctly is much more difficult. Or, from MSDN:

When you want a responsive UI and you are faced with long delays associated with such operations, the BackgroundWorker class provides a convenient solution.

北城孤痞 2024-10-15 22:14:41

我对 BackgroundWorker、异步委托和其他方法进行了简要比较 在我的博客上(从做后台操作的角度来看)。

BackgroundWorker 具有以下优点:

  • 进度报告很容易。任何 WorkerSupportsProgress 属性为 true 的 BackgroundWorker 都可以报告进度。 DoWork 委托可以调用 ReportProgress,这会导致 ProgressChanged 事件触发。
  • 有一个内置的合作取消系统。取消线程首先调用BackgroundWorker.CancelAsync。这会导致 BackgroundWorker.CancellationPending 属性变为 true。 DoWork 委托应监视该属性(定期检查),并将 DoWorkEventArgs.Cancel 设置为 true,并在操作取消时返回。 RunWorkerCompleted 委托通过检查 RunWorkerCompletedEventArgs.Cancelled 来检测取消的结果。
  • 完成指示和进度报告的同步是自动的。 ProgressChangedRunWorkerCompleted 事件同步到调用 RunWorkerAsync 时到位的 SynchronizationContext

异步委托有以下优点:

  • 返回值很简单;它刚刚返回。

总之,我建议使用 Task 而不是 BackgroundWorker 或异步委托。

I have a brief comparison of BackgroundWorker, asynchronous delegates, and other approaches on my blog (from the perspective of doing background operations).

BackgroundWorker has these advantages:

  • Progress reporting is easy. Any BackgroundWorker whose WorkerSupportsProgress property is true may report progress. The DoWork delegate may invoke ReportProgress, which causes the ProgressChanged event to fire.
  • There's a built-in system for cooperative cancellation. The cancelling thread first calls BackgroundWorker.CancelAsync. This causes the BackgroundWorker.CancellationPending property to become true. The DoWork delegate should monitor that property (checking it on a regular basis), and set DoWorkEventArgs.Cancel to true and return if the operation is cancelled. The RunWorkerCompleted delegate detects a cancelled result by checking RunWorkerCompletedEventArgs.Cancelled.
  • Synchronization is automatic, for both completion indications and progress reports. The ProgressChanged and RunWorkerCompleted events are synchronized to the SynchronizationContext that was in place when RunWorkerAsync was called.

Asynchronous delegates have this advantage:

  • Returning a value is straightforward; it's just returned.

In conclusion, I recommend using Task<TResult> instead of either BackgroundWorker or asynchronous delegates.

俯瞰星空 2024-10-15 22:14:41

后台工作线程主要用于 UI 工作,您需要在后台线程上轻松运行任务并向屏幕提供进度更新。

一个优点是它为您将其回调编组到 UI 线程,因此您无需检查是否 InvokeRequired 等。

委托是一种更通用的机制,用于将函数作为参数传递,并且通过异步执行它们,它们为您提供了一种运行这些函数的简单方法另一个线程上的方法。

The background worker is primarily for UI work where you need to easily run a task on a background thread and provide progress updates to the screen.

One advantage is it marshalls its callbacks to the UI thread for you so you don't need to check if InvokeRequired etc.

Delegates are a more general mechanism for passing functions as arguments and by executing them asynchronously they give you an easy way of running those methods on another thread.

为人所爱 2024-10-15 22:14:41

后台工作是一个抽象,可帮助您在单独的线程上执行操作。

委托并不真正启动单独的线程 - 它们是一种引用方法的类型。

但您可能感兴趣的是何时应该使用异步方法而不是使用后台工作程序。我在这方面没有太多经验,但是 Anders Hejlsberg 在他的文章中对此进行了一些讨论关于 C# 的未来的 PDC 会议

我得到的消息是,在某些情况下,异步方法会更好,因为复杂性较低。 UI 线程仍然会被阻塞,但时间不会太长,以至于产生影响。

Background worker is an abstraction to help you execute an operation on a separate thread.

Delegates don't really start separate threads - they are a type for referring to methods.

But what you are probably interested in is when you should use asynchronous methods instead of using background worker. I have not much experience in this, but Anders Hejlsberg talked about it some in his PDC session on the Future of C#.

The message I got was that in some circumstances asynchronous methods would be preferable because of lower complexity. The UI thread would still be blocked, but not for so long that it mattered.

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