用RegisterWaitHandle和AutoResetEvent来判断线程池是否执行完毕。
RegisteredWaitHandle registeredWaitHandle = null;
registeredWaitHandle = ThreadPool.RegisterWaitForSingleObject(new AutoResetEvent(false), new WaitOrTimerCallback(delegate(object obj, bool timeout)
{
int workerThreads = 0;
int maxWordThreads = 0;
int compleThreads = 0;
ThreadPool.GetAvailableThreads(out workerThreads, out compleThreads);
ThreadPool.GetMaxThreads(out maxWordThreads, out compleThreads);
if (workerThreads == maxWordThreads)
{
Thread.Sleep(10000);
mainAutoResetEvent.Set();
registeredWaitHandle.Unregister(null);
}
}), null, 1000, false);