I have several Threads running in a ThreadPool in my WPF App. I need to monitor them, possibly from another Thread to find out if all of those Threads have terminated.
How would I be able to achieve this.
For simplicity, my usage scenario is:
- Click a button
- Start several Threads in a ThreadPool.
- Start another Thread to Monitor those in a ThreadPool
- Lock UI of WPF App.
- Release Lock when the Thread in step 3 sets a Global value to indicate that Threads started in (2) have terminated.
The problem at hand is actually too localized. I had to adopt to an Ad Hoc approach to fit in with the initial flawed design.
My approach was to simply increase a counter whenever a Thread is started and decrease the value of the counter when that Thread terminates. To watch over the value of counter, I initiated another Thread simply to monitor the value of counter. Maybe not an elegant approach, but seems to work fine.