zoukankan      html  css  js  c++  java
  • 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));
    
            }
        }
    }
  • 相关阅读:
    技术为辅,思维主导
    阶段性目标的设置
    非计算机专业测试之路
    第四章 Appium真机运行测试用例讲解
    第三章 Appium API介绍
    第二章 测试环境搭建(下)
    第二章 测试环境搭建(上)
    第一章 Appium简介
    测试人员的工作经验值钱吗
    2017 年该学习的编程语言、框架和工具
  • 原文地址:https://www.cnblogs.com/rosizel/p/3857311.html
Copyright © 2011-2022 走看看