开发者

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:

  1. Click a button
  2. Start several Threads in a ThreadPool.
  3. Start another Thread to Monitor those in a ThreadPool
  4. Lock UI of WPF App.
  5. 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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜