It is actually very easy to use ThreadPool. the RegisterWaitForSingleObject method registers a method for regularly checking the thread pool and calls ThreadPool within the method of checking the thread. getAvailableThreads and ThreadPool. getMaxThreads and compare whether the values returned by the two methods are equal. Equal means that all threads in the thread pool have been completed.
// Check the status of the thread pool once per second
RegisteredWaitHandle rhw = ThreadPool. RegisterWaitForSingleObject (AutoResetEvent (false), this. CheckThreadPool, null, 1000, false );
// Method for checking the thread pool
Private void CheckThreadPool (object state, bool timeout)
{
Int workerThreads = 0;
Int maxWordThreads = 0;
// Int
Int compleThreads = 0;
ThreadPool. GetAvailableThreads (out workerThreads, out compleThreads );
ThreadPool. GetMaxThreads (out maxWordThreads, out compleThreads );
// When the number of available threads is equal to the maximum number of threads in the pool, all threads in the pool have been completed.
If (workerThreads = maxWordThreads)
{
// When this method is executed, the CheckThreadPool will no longer execute
Rhw. Unregister (null );
// Add the processing code after all threads are completed.
}
}