如何让BackgroundWorker返回一个对象
我需要使 RunWorkerAsync()
返回 List
。
能够从后台工作者返回对象的过程是什么?
I need to make RunWorkerAsync()
return a List<FileInfo>
.
What is the process to be able to return an object from a background worker?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
在
BackgroundWorker
(后台工作发生的地方)的DoWork
事件处理程序中,有一个参数DoWorkEventArgs
。 该对象有一个公共属性对象 Result。 当您的工作线程生成其结果(在您的情况下为List
)时,将e.Result
设置为该结果,然后返回。现在您的BackgroundWorker 已完成其任务,它会触发
RunWorkerCompleted
事件,该事件有一个RunWorkerCompletedEventArgs
对象作为参数。RunWorkerCompletedEventArgs.Result
将包含来自BackgroundWorker
的结果。例子:
In your
DoWork
event handler for theBackgroundWorker
(which is where the background work takes place) there is an argumentDoWorkEventArgs
. This object has a public property object Result. When your worker has generated its result (in your case, aList<FileInfo>
), sete.Result
to that, and return.Now that your BackgroundWorker has completed its task, it triggers the
RunWorkerCompleted
event, which has aRunWorkerCompletedEventArgs
object as an argument.RunWorkerCompletedEventArgs.Result
will contain the result from yourBackgroundWorker
.example:
我假设您不想阻止并等待 RunWorkerAsync() 的结果(如果您这样做,就没有理由运行异步!
如果您想在后台进程完成时收到通知,请挂钩 RunWorkerCompleted 。如果要返回某些状态,请在 DoWork 事件参数的 Result 成员中返回它。
事件
I'm assuming that you don't want to block and wait on RunWorkerAsync() for the results (if you did, there would be no reason to run async!
If you want to be notified when the background process finishes, hook the RunWorkerCompleted Event. If you want to return some state, return it in the Result member of DoWork's event args.
Example:
RunWorkerAsync()
异步启动进程,并在进程实际完成之前返回并继续执行代码。 如果您想获取BackgroundWorker
的结果,则需要创建一个实例变量来保存该值,并在BackgroundWorker
完成后检查它。如果您想等到工作完成,那么您不需要
BackgroundWorker
。RunWorkerAsync()
starts the process asynchronously and will return and continue executing your code before the process actually completes. If you want to obtain the result of theBackgroundWorker
, you'll need to create an instance variable to hold that value and check it once theBackgroundWorker
completes.If you want to wait until the work is finished, then you don't need a
BackgroundWorker
.为了补充大卫的答案,人们可能想要推送一个元组来为方法提供多个参数。
为此,让我更新他的答案,其中通过每个调用传递一个值(称为engagementId),并且元组保存要使用的原始项目以及结果。
有关详细信息,请参阅如何转换为元组的答案。
To add to David's answer, one may want to push a tuple through to provide more than one argument to the methods.
To do so let me update his answer, where a value (called engagementId) is passed through each of the calls and the tuple holds that original item for use as well as the result.
See the answer to How To Cast To A Tuple for more information.
根据您的模型,您要么希望工作线程在完成工作时回调到其创建者(或其他进程),要么必须经常轮询工作线程以查看它是否已完成,如果所以,得到结果。
等待工作线程返回其结果的想法破坏了多线程的好处。
Depending on your model, you either want to have your worker thread call back to its creator (or to some other process) when it's finished its work, or you have to poll the worker thread every so often to see if it's done and, if so, get the result.
The idea of waiting for a worker thread to return its result undermines the benefits of multithreading.
您可以让您的线程引发一个以对象作为参数的事件:
其中:
You could have your thread raise an event with the object as an argument:
where:
一般来说,当异步运行进程时,工作线程应该调用委托或触发事件(如 ChrisF)。
您可以查看新的 PFX,它具有一些可以返回值的并发函数。
例如,有一个名为 Parallel.ForEach() 的函数,它具有可以返回值的重载。
查看此内容以获取更多信息
http://msdn.microsoft.com/en-us/magazine/cc817396.aspx
Generally speaking when running a process async, The worker thread should call a delegate or fire an event (like ChrisF).
You can check out the new PFX which has some concurrency function that can return values.
For example there is a function called Parallel.ForEach() which has an overload that can return a value.
check this out for more info
http://msdn.microsoft.com/en-us/magazine/cc817396.aspx
不要在“DoWork”方法中进行后台工作,而是创建一个返回您想要返回的类型的方法,并将其应用到 e.Result,如此处建议的其他答案所示。 作为回答 OP 问题而不使问题过于复杂的最小示例...
该示例还演示了如何从 BackgroundWorker API 更新 TextBox。 未显示的是对通过 ProgressBar 和 TextBoxes 报告进度的支持以及对取消的支持,API 也支持这两者。 该代码是通过按钮运行的...
Instead of doing the background work in the "DoWork" method, create a method that returns the type you want to return and apply that to e.Result as other answers here recommend. As a minimal example that answers the OP's question without overcomplicating the matter...
The example also shows how to update a TextBox from the BackgroundWorker API. Not shown is support for reporting progress via a ProgressBar and TextBoxes and support for cancellation, both are also supported in the API. The code is ran via a button...