I need a few threads working simultaneously with URL requests in a VB.NET 2010 app. Once one of them finishes, it should continue with the next task in a list.
I’m new to using threads. Since it seems like I can’t declare an array of BackgroundWorker, I thought I’d have a control class that creates an instance of a task class for each task, which in turn starts a BackgroundWorker in its constructor.
However, with this approach I’m not sure how to report the result back to the control class upon completion. I had this vague idea that I could send a reference to an object in the control class (or to a public event I can trigger, is this possible?), but then I have to somehow store this address until the work is done. And I don’t know how I can store the address from a ByRef for future use.
What I would basically like to know is: What is the standard pattern for accomplishing this, how should I think, can anyone point to an example?
Thanks!
You can. And in you case, it looks like you absolutely should, since the
BackgroundWorkeroffers exactly the functionality you need.Just declare the worker in code, rather than via the Forms Designer: The Forms Designer doesn’t support arrays of controls/components. But in code, no such restriction exists. The only problem is that you cannot declare lists/arrays/… as
WithEvents. You need to register and deregister event handlers manually on all instances, usingAddHandlerandRemoveHandler.