开发者

How should threads update global data in the main program?

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 t开发者_Python百科hreads).

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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜