首页 > 代码库 > ThreadPool

ThreadPool

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading;using System.Windows.Forms;using WindowsFormsApplication3.Enums;using WindowsFormsApplication3.DataStorge;using WindowsFormsApplication3.Jobs;using WindowsFormsApplication3.Jobs.Operation_System;namespace WindowsFormsApplication3.FormPage{    public partial class ThreadPoolTest : BaseWorkerForm    {        protected int finishedChildFormNO;        protected CheckoutStep checkoutStep;        void execute_StateChangeEvent(string strTipInfo, int Number, bool IsEnd)        {                     if (this.InvokeRequired)            {                this.BeginInvoke(new ExeCuteManager.DelArgInfo(DelPolfun), new object[] { strTipInfo, Number, IsEnd });            }            else            {                DelPolfun(strTipInfo, Number, IsEnd);            }        }        private void DelPolfun(string strTipInfo, int Number, bool IsEnd)        {            this.listBox.Items.Add(strTipInfo);            if (Number == 3 && IsEnd)            {                this.listBox.Items.Add(string.Format("所有的线程执行结束!"));            }        }        public ThreadPoolTest(CheckoutContext checkoutContext, CheckoutStep checkoutStep)            : base(checkoutContext, checkoutStep)        {            this.checkoutStep = checkoutStep;            InitializeComponent();            ResetForm();            backgroundWorker.DoWork += backgroundWorker_DoWork;            backgroundWorker.RunWorkerCompleted += backgroundWorker_RunWorkerCompleted;        }        private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)        {            bool isErrorFound = false;             foreach (BaseJob job in jobs)            {                job.StateChangeEvent += execute_StateChangeEvent;                WaitCallback callBack = new WaitCallback(job.Execute);                ThreadPool.QueueUserWorkItem(callBack, 1);                ThreadPool.QueueUserWorkItem(callBack, 2);                ThreadPool.QueueUserWorkItem(callBack, 3);                if (job.Status == JobStatus.Error)                {                    isErrorFound = true;                    checkoutContext.logData.Error(checkoutStep, "Job error");                }                status = isErrorFound ? FormStatus.Error : FormStatus.OK;                ReportStatusChanged();            }        }        private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)        {            timer.Stop();            timer.Enabled = false;            RefreshForm();            ReportStatusChanged();            checkoutContext.logData.Finish(checkoutStep, "");        }        public override void Execute()        {            ReportStatusChanged();            checkoutContext.logData.Start(checkoutStep, "");            base.Execute();            backgroundWorker.RunWorkerAsync();        }        public override void ResetForm()        {            base.ResetForm();            jobs.Add(new ExeCuteManager(checkoutContext,checkoutStep));        }    }}