How to make BackgroundWorker return an object

2019-01-10 12:05发布

I need to make RunWorkerAsync() return a List<FileInfo>. How can I return an object from a background worker?

6条回答
Luminary・发光体
2楼-- · 2019-01-10 12:46

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.

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...
    }


查看更多
一纸荒年 Trace。
3楼-- · 2019-01-10 12:50

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.

查看更多
The star\"
4楼-- · 2019-01-10 12:58

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;
    }
}
查看更多
Bombasti
5楼-- · 2019-01-10 12:58

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

查看更多
戒情不戒烟
6楼-- · 2019-01-10 13:02

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.

If you want to wait until the work is finished, then you don't need a BackgroundWorker.

查看更多
够拽才男人
7楼-- · 2019-01-10 13:03

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.

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.

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());
}
查看更多
登录 后发表回答