我创建了一些线程来使用线程池进行一些工作。每个线程都会增加 finishedThreads 变量,因此主线程会知道所有线程池线程何时终止:
// in the main thread
while (finishedThreads < threadsNumber) {
// wait
}
// threads terminated, we can continue
// last line of the threadpool thread
++finishedThreads;
一切正常,直到创建大量线程(超过50个)为止。最后一个线程永不终止,因此 finishedThreads 仍然等于 threadsNumber-1 ,并且主线程永远不会继续。我试图找出原因,使用调试,停止Visual等,但没有任何帮助。该线程没有终止,尽管如Visual所示,它不执行任何代码。您对出什么问题有任何想法吗?预先感谢。
[EDIT]:这就是我创建新线程的方式:
ThreadPool.QueueUserWorkItem(new WaitCallback(myThreadFunc), someData);