How to Monitor C# Threads from another Thread
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 simplic开发者_如何学Pythonity, 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.
Put all the threads in the threadpool in an array. When you want to wait for the pool threads to exit, use WaitHandle.WaitAll
to wait for the threads to complete.
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.
精彩评论