Click to See Complete Forum and Search --> : Threading Concepts.


somu0915
May 28th, 2008, 03:16 AM
Hi I am creating two threads in my main method.
One: UI Thread which runs the UIMethod.
Second: Background Thread which runs the BackGroundMethod.

Now in the BackGroundMethod, I am calling several other methods which create many many threads and do lot of background work.
How can I know in the Main Method, when all of the entire threads created have terminated?

If I use Join method, then it would wait only for the BackgroundMethod to finish and not all the threads in the BackGroundMethod.

Can anyone help me on this??

cjard
May 29th, 2008, 06:18 AM
If you use Join, it will lock up your UI until the background thread finishes, which kinda makes use of a background thread rather pointless

If backgroundthread spawns 1000 other threads and you want the UI to show this, you should maybe consider embodying it as a Worker of some kind (how about backgroundworker ?) :)
And raise an event every time it starts a new thread.

Alternately, if some class is responsible for managing the worker threads, it should provide for the ability to be interrogated or raise events about the number of thread sit is managing

Arjay
May 29th, 2008, 11:38 AM
Another option to spawning the threads yourself is to use ThreadPool.QueueUserWorkItem.

You can create a background thread, and this thread controls sending 'jobs' the the QueueUserWorkItem method. This method takes a delegate so when you push the job onto the queue, you create and pass in an event (and store each event into an array). The thread proc function sets this event when it's finished with the work and. After pushing all the 'job's onto the QueueUserWorkItem, the background thread justs waits on the events in the array.