首页 > 代码库 > 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)); } }}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。