C # how to determine whether all threads in the thread pool have been completed

Source: Internet
Author: User

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.

      

}


}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.