I need to make RunWorkerAsync()
return a List<FileInfo>
. How can I return an object from a background worker?
我需要让RunWorkerAsync()返回List
6 个解决方案
#1
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.
在BackgroundWorker的DoWork事件处理程序中(后台工作发生的地方)有一个参数DoWorkEventArgs。该对象具有公共属性对象Result。当您的worker生成其结果(在您的情况下,List
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());
}
#2
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...
}
#3
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。
#4
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;
}
}
#5
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.
等待工作线程返回其结果的想法破坏了多线程的好处。
#6
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
看看这个以获取更多信息
#1
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.
在BackgroundWorker的DoWork事件处理程序中(后台工作发生的地方)有一个参数DoWorkEventArgs。该对象具有公共属性对象Result。当您的worker生成其结果(在您的情况下,List
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());
}
#2
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...
}
#3
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。
#4
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;
}
}
#5
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.
等待工作线程返回其结果的想法破坏了多线程的好处。
#6
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
看看这个以获取更多信息