I am revisiting an old thread of mine.
I want to launch a bunch of threads, each performing the same task, and know in main() when each finishs and it if it was successful or failed.
The solution offered was to use a ConcurrentQueue, but other posts have recommended using a BackgroundWorker Class, or a thread pool.
Is there a definitive answer?
Again, all threads perform the same code and have a pass/fail result. I want to run more than there are available threads so as soon as one thread finishes I will launch another asap - I want tehm to stress a remote systems as much as possible (reather than stressing my local PC with too many threads, so I will need to experiment to determine the optimal number of threads).
VB .NET for specific answer, but general threading advice is also welcome.
BackgroundWorker is a very easy way to manage your running threads. It allows you to report progress back to the UI easily. I don't think there is a definitive answer but BackgroundWorker is designed for this purpose - running background tasks which update the UI as they progress. Here is an example of how to is it.
Josh's is the way I'd go.
Create as many new backgroundworkers as you need, hook up to intercept their "finished" event (I forget the exact name), and when those events fire back on the main app thread, store the results in a collection/list/array of somesort.
Things can get interesting if you really need to pool threads so you don't just create a ton of threads which can cause a lot of context thrashing. Instead, you want to have a pool of, say 2x(the number of processors in the machine), and as each backgroundworker finishes, queue up another task in it's place.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With