C# 如何让 BackgroundWorker 返回一个对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/939635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make BackgroundWorker return an object
提问by
I need to make RunWorkerAsync()
return a List<FileInfo>
. How can I return an object from a background worker?
我需要RunWorkerAsync()
返回一个List<FileInfo>
. 如何从后台工作人员返回对象?
回答by Welbog
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.
等待工作线程返回其结果的想法破坏了多线程的好处。
回答by Adam Robinson
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 the BackgroundWorker
, you'll need to create an instance variable to hold that value and check it once the BackgroundWorker
completes.
RunWorkerAsync()
异步启动进程,并在进程实际完成之前返回并继续执行您的代码。如果您想获得 的结果BackgroundWorker
,您需要创建一个实例变量来保存该值并在BackgroundWorker
完成后检查它。
If you want to wait until the work is finished, then you don't need a BackgroundWorker
.
如果您想等到工作完成,那么您不需要BackgroundWorker
.
回答by ChrisF
You could have your thread raise an event with the object as an argument:
你可以让你的线程以对象作为参数引发一个事件:
ThreadFinishedEvent(this, new ThreadEventArgs(object));
where:
在哪里:
public class ThreadEventArgs : EventArgs
{
public ThreadEventArgs(object object)
{
Object = object
}
public object Object
{
get; private set;
}
}
回答by JMarsch
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!
我假设您不想阻塞并等待 RunWorkerAsync() 的结果(如果您这样做了,就没有理由运行异步!
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.
如果您想在后台进程完成时收到通知,请挂钩 RunWorkerCompleted 事件。如果您想返回某个状态,请在 DoWork 的事件参数的 Result 成员中返回它。
EDIT: I posted prematurely -- finished my code example
编辑:我过早发布 - 完成了我的代码示例
Example:
例子:
private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
// do your thing
....
// return results
e.Result = theResultObject;
}
// now get your results
private void BackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
MyResultObject result = (MyResultObject)e.Result;
// process your result...
}
回答by David
In your DoWork
event handler for the BackgroundWorker
(which is where the background work takes place) there is an argument DoWorkEventArgs
. This object has a public property object Result. When your worker has generated its result (in your case, a List<FileInfo>
), set e.Result
to that, and return.
在您的DoWork
事件处理程序中BackgroundWorker
(这是后台工作发生的地方),有一个参数DoWorkEventArgs
。这个对象有一个公共属性对象 Result。当您的工作人员生成其结果(在您的情况下为 a List<FileInfo>
)时,设置e.Result
为该结果,然后返回。
Now that your BackgroundWorker has completed its task, it triggers the RunWorkerCompleted
event, which has a RunWorkerCompletedEventArgs
object as an argument. RunWorkerCompletedEventArgs.Result
will contain the result from your BackgroundWorker
.
现在你的 BackgroundWorker 已经完成了它的任务,它触发了一个RunWorkerCompleted
事件,它有一个RunWorkerCompletedEventArgs
对象作为参数。RunWorkerCompletedEventArgs.Result
将包含您的BackgroundWorker
.
example:
例子:
private void bgw_DoWork(object sender, DoWorkEventArgs e)
{
int result = 2+2;
e.Result = result;
}
private void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
int result = (int)e.Result;
MessageBox.Show("Result received: " + result.ToString());
}
回答by Sruly
Generally speaking when running a process async, The worker thread should call a delegate or fire an event (like ChrisF).
一般来说,在异步运行进程时,工作线程应该调用委托或触发事件(如 ChrisF)。
You can check out the new PFX which has some concurrency function that can return values.
您可以查看具有一些可以返回值的并发函数的新 PFX。
For example there is a function called Parallel.ForEach() which has an overload that can return a value.
例如,有一个名为 Parallel.ForEach() 的函数,它有一个可以返回值的重载。
check this out for more info
查看更多信息