Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you notify a parent thread that all child threads have terminated?

I have a console app that I'm porting to WPF. The application has 3 worker threads, that are all joined to the main thread before some output results are printed to the screen. My understanding is that, if I try and do the same thing in a WPF application, the GUI will be blocked and will not be reponsive to the user. How then can I notify the parent thread that all the threads have completed their work? I think the solution is going to involve delegates and events (or maybe BackgroundWorker?), but it was not clear to me how to get the callback invoked when the thread terminated.

Original Code:

foreach (Thread t in threadList)
{
                t.Start();
}

foreach (Thread t in threadList)
{
                t.Join();
}

// print some results here
like image 730
Andrew Avatar asked Jun 04 '10 14:06

Andrew


People also ask

What happens to child thread if parent dies?

Here's another example which shows that if the parent thread dies, the child threads will also die.

Does child threads still execute after even if their parent thread dies or terminates?

Because if parent thread dies or terminates then child thread should also die or terminate.

How do you stop a child thread in Java?

Modern ways to suspend/stop a thread are by using a boolean flag and Thread. interrupt() method. Using a boolean flag: We can define a boolean variable which is used for stopping/killing threads say 'exit'. Whenever we want to stop a thread, the 'exit' variable will be set to true.

Do threads have parent/child relationship?

Unlike Unix process, plain Haskell thread, created by forkIO, has no parent-child relation each other. This means termination of parent thread doesn't result its children also terminated.


1 Answers

If you are using three BackgroundWorkers, you can use the event RunWorkerCompleted to notice that one of the workers is completed: Before starting the workers set a counter to 3 then decrement and check this counter in the method called by RunWorkerCompleted if it hits 0 you are finished.

like image 54
Hinek Avatar answered Sep 23 '22 04:09

Hinek