Alex Rivera | Logout

Basic BackgroundWorker usage with parameters

Asked 2011-04-26T17:52:01.980
21

My process intensive method call that I want to perform in a background thread looks like this:

object.Method(paramObj, paramObj2);

All three of these objects are ones I have created. Now, from the initial examples I have seen, you can pass an object into a backgroundworker's DoWork method. But how should I go about doing this if I need to pass additional parameters to that object, like I'm doing here? I could wrap this in a single object and be done with it, but I thought it would be smart to get someone else's input on this.

Edit
Report

1 Answer

1

You can pass an Object into the BackgroundWorker.RunWorkerAsync() method. You should wrap your two paramObjs into one Object (you could use an array, a tuple, or some other composite class that you would write) and pass that as the argument into RunWorkerAsync().

Then, when your DoWork event is raised, you can retrieve this values by looking at the Argument property of the DoWorkEventArgs parameter of the event handler.

For a complete example, check out on MSDN: http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker%28v=VS.90%29.aspx

answered 2011-04-26T17:56:16.697

Your Answer