private void button6_Click(object sender, EventArgs e)
        {
            ThreadPool.SetMinThreads(, );
            ThreadPool.SetMaxThreads(, );
            //可以用ManualResetEvent控制
            //var ehs = new List<ManualResetEvent>();
            ; i < ; i++)
            {
                //var m = new ManualResetEvent(false);
                //m.Add(m);
                //可以封装到类,也可直接传索引
                ThreadHelper th = new ThreadHelper();
                th.id = i;
                //也可以原子
                //Interlocked.Increment(ref i);
                ThreadPool.QueueUserWorkItem(new WaitCallback(work3), th);
                ThreadPool.QueueUserWorkItem(h =>
                {
                    work3(th);
                    //m.Set();
                });
            }
            ////第一种方式等待
            //WaitHandle.WaitAll(ehs.ToArray());
            ////第二种方式等待
            //foreach (var m in ehs)
            //{
            //    m.WaitOne();
            //}
            while (!CheckThreadPool())
            { Application.DoEvents(); }
            MessageBox.Show("投递~~~~~");
        }

        /// <summary>
        /// 取是否空闲
        /// </summary>
        /// <returns></returns>
        private bool CheckThreadPool()
        {
            ;
            ;
            ;
            ThreadPool.GetAvailableThreads(out workerThreads, out compleThreads);
            ThreadPool.GetMaxThreads(out maxWordThreads, out compleThreads);
            //当可用的线数与池程池最大的线程相等时表示线程池中所有的线程已经完成
            if (workerThreads == maxWordThreads)
            {
                //当执行此方法后CheckThreadPool将不再执行
                return true;
                //此处加入所有线程完成后的处理代码
            }
            return false;
        }
        /// <summary>
        /// 取是否空闲
        /// </summary>
        /// <returns></returns>
        private bool GetWhetherIdle()
        {
            int MaxWorkerThreads, miot, AvailableWorkerThreads, aiot;

            //获得最大的线程数量
            ThreadPool.GetMaxThreads(out MaxWorkerThreads, out miot);

            AvailableWorkerThreads = aiot = ;

            //获得可用的线程数量
            ThreadPool.GetAvailableThreads(out AvailableWorkerThreads, out aiot);

            //返回线程池中活动的线程数
            return MaxWorkerThreads == AvailableWorkerThreads;
        }
        /// <summary>
        /// 取空闲线程数
        /// </summary>
        /// <returns></returns>
        private int GetIdle()
        {
            int MaxWorkerThreads, miot, AvailableWorkerThreads, aiot, threadcount;

            //获得最大的线程数量
            ThreadPool.GetMaxThreads(out MaxWorkerThreads, out miot);

            AvailableWorkerThreads = aiot = ;

            //获得可用的线程数量
            ThreadPool.GetAvailableThreads(out AvailableWorkerThreads, out aiot);

            //取线程池线程数
            ThreadPool.GetMinThreads(out threadcount, out threadcount);
            //返回线程池中活动的线程数
            return threadcount - (MaxWorkerThreads - AvailableWorkerThreads);
        }
        private void work3(object task)
        {

            //可以封装到类,也可直接传索引
            ThreadHelper task2 = task as ThreadHelper;
            this.Invoke(new MethodInvoker(delegate
            {
                textBox1.AppendText(task2.id.ToString() + "\r\n");
            }));
            Thread.Sleep();
        }
05-06 06:20