C#线程池使用

        bool stop;
        private void button3_Click(object sender, EventArgs e)
        {
            if(button3.Text=="线程池")
            {
                stop = false;
                button3.Text = "停止操作";
                ThreadPool.SetMaxThreads(Convert.ToInt32(numericUpDown1.Value), Convert.ToInt32(numericUpDown1.Value));
                ; i < ; i++)
                {
                    //可以封装到类,也可直接传索引
                    TaskObject task = new TaskObject(i);
                    ThreadPool.QueueUserWorkItem(new WaitCallback(work3), i);
                }
                while (!CheckThreadPool())
                { Application.DoEvents(); }
                button3.Text = "线程池";
            }
            else
            {
                stop = true;
                button3.Text = "停止中";
            }
        }
        private void work3(Object task)
        {
            if(stop){return;}
            //可以封装到类,也可直接传索引
            TaskObject task2 = task as TaskObject;
            this.Invoke(new MethodInvoker(delegate
            {
                textBox1.AppendText(task.ToString() + "\r\n");
            }));
            Thread.Sleep();
        }
        private bool CheckThreadPool()
        {
            ;
            ;
            ;
            ThreadPool.GetAvailableThreads(out workerThreads, out compleThreads);
            ThreadPool.GetMaxThreads(out maxWordThreads, out compleThreads);
            //当可用的线数与池程池最大的线程相等时表示线程池中所有的线程已经完成
            if (workerThreads == maxWordThreads)
            {
                //当执行此方法后CheckThreadPool将不再执行
                return true;
                //此处加入所有线程完成后的处理代码
            }
            return false;
        }
上一篇:MYSQL安装与卸载(一)


下一篇:centos7下mysql安装与卸载