4

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.

4 Answers 4

5
+25

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.

1
  • I added links to more information but yes, behind the scenes it is launching a thread to do the work.
    – Josh M.
    Mar 22, 2011 at 13:48
1

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.

1

It depends. In a Windows Forms application I would go with BackgroundWorker, because it is very easy to setup and use and it saves me a lot of headache looking for incorrect cross-threading bugs.

But you must be aware that BackgroundWorker only works correctly in an environment where there is an UI thread and a message loop. So it will not work in console application, for instance.

In that case you should use some other solution, Thread, ThreadPool or some kind of framework.

1
  • +1 thanks. Peformance is vital, soI need a fast solution, even at the expense of a simple solution. Do you know thw class hierarchy? Is backgroundworker derived from thread?
    – Mawg
    Mar 22, 2011 at 22:51
1

I agree with the propositions about BackgroundWorker. However, if you need to access COM objects from the background threads do not use the BackgroundWorker but create your own threading mechanismo and use MTA STA model as required.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.