委托与后台工作人员的区别?
谁能解释一下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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
BackgroundWorker
:委托
:使用哪一个的问题与效率无关。
BackgroundWorker
是一个简化线程使用的包装器,您也可以使用异步委托,但正确管理它们要困难得多。或者,来自 MSDN:BackgroundWorker
:Delegate
: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:我对
BackgroundWorker
、异步委托和其他方法进行了简要比较 在我的博客上(从做后台操作的角度来看)。BackgroundWorker
具有以下优点:WorkerSupportsProgress
属性为 true 的BackgroundWorker
都可以报告进度。DoWork
委托可以调用ReportProgress
,这会导致ProgressChanged
事件触发。BackgroundWorker.CancellationPending
属性变为 true。DoWork
委托应监视该属性(定期检查),并将DoWorkEventArgs.Cancel
设置为 true,并在操作取消时返回。RunWorkerCompleted
委托通过检查RunWorkerCompletedEventArgs.Cancelled
来检测取消的结果。ProgressChanged
和RunWorkerCompleted
事件同步到调用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:BackgroundWorker
whoseWorkerSupportsProgress
property is true may report progress. TheDoWork
delegate may invokeReportProgress
, which causes theProgressChanged
event to fire.BackgroundWorker.CancelAsync
. This causes theBackgroundWorker.CancellationPending
property to become true. TheDoWork
delegate should monitor that property (checking it on a regular basis), and setDoWorkEventArgs.Cancel
to true and return if the operation is cancelled. TheRunWorkerCompleted
delegate detects a cancelled result by checkingRunWorkerCompletedEventArgs.Cancelled
.ProgressChanged
andRunWorkerCompleted
events are synchronized to theSynchronizationContext
that was in place whenRunWorkerAsync
was called.Asynchronous delegates have this advantage:
In conclusion, I recommend using
Task<TResult>
instead of eitherBackgroundWorker
or asynchronous delegates.后台工作线程主要用于 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.
后台工作是一个抽象,可帮助您在单独的线程上执行操作。
委托并不真正启动单独的线程 - 它们是一种引用方法的类型。
但您可能感兴趣的是何时应该使用异步方法而不是使用后台工作程序。我在这方面没有太多经验,但是 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.