• 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));
    
            }
        }
    }
  • 相关阅读:
    c++ 获取某个进程个数
    Hadoop集群配置【三、多节点部署】
    *** glibc detected *** [没事写写Bug]
    Mahout in action 中文版3.推荐器的数据表达3.2
    simHash原理【转】
    Mahout in action 中文版2.推荐器的介绍2.4~2.6
    Hadoop集群配置【四、Hbase部署】
    mongodb driver 编译失败,各种undefine
    Mahout in action 中文版2.推荐器的介绍2.1~2.2
    Mahout in action 中文版3.推荐器的数据表达3.3~3.4
  • 原文地址:https://www.cnblogs.com/rosizel/p/3857311.html
Copyright © 2020-2023  润新知