C ා thread pool


Why use thread pool?

There is no need to repeatedly open a new destroy thread

Environment: vs 2015. Net 4.5.2 output environment: console application

Post code:

C ා thread poolC ා thread pool

class Program
        Static int workerthreads; // the maximum number of worker threads in the thread pool
        Static int completionportthreads; // the maximum number of asynchronous I / O threads in the thread pool

        Static AutoResetEvent myevent = new AutoResetEvent (false); // default blocking
        static void Main(string[] args)
            Console.WriteLine("Begin in Main");

            ThreadPool.GetMaxThreads(out workerThreads, out completionPortThreads);

            Console.WriteLine($"workerThreads:{workerThreads} completionPortThreads:{completionPortThreads}");

            ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadDo));

            Console.WriteLine("End in Main");

            myEvent.WaitOne (); // blocking condition 1. The signal point AutoResetEvent is false; 2 myEvent.Set ();

            Console.WriteLine ("thread pool terminated!");

        static void ThreadDo(Object o)
            for (int i = 0; i < 10; i++)
                Console.WriteLine(DateTime.Now.ToString() + "[Thread " + (int)i + "] Execute in ThreadDo");
            myEvent.Set (); // assign a value to the semaphore to let the UI thread continue to execute without assigning UI infinite wait

View Code