控制固定线程个数 循环添加

实现控制 6个线程,分别对应6个窗口。
每关闭一个子窗口,则自动开启一个新窗口(保证固定线程的数量)

  --- 父窗口 -------

 public partial class Form1 : Form
    {
        int a; 
        public Form1()
        {
            InitializeComponent();
            a = 0; 
        }
        
        Thread t1;
        private void button1_Click(object sender, EventArgs e)
        {
            while (a < (int)numericUpDown1.Value)
            {
                t1 = new Thread(SetInfo1);
                t1.Start();
                a++;
            }
        }

        public void SetInfo1()
        {
            chil frm = new chil(this);
            frm.changetext_event += new chil.changetext(frm_changetext_event);
            frm.ShowDialog(); 
        }
        private void frm_changetext_event(string text)
        {
            a--;
            this.button1_Click(null,null);
        }
 
    }

  --- 子窗口 ----

 public partial class chil : Form
    {
        public delegate void changetext(string text);
         public event changetext changetext_event;
        public chil( Form1 f)
        {
            InitializeComponent();
            label1.Text = DateTime.Now.ToLocalTime().ToString();
        }

        private void chil_FormClosing(object sender, FormClosingEventArgs e)
        {
            changetext_event(""); 
        }
    }

  

原文地址:https://www.cnblogs.com/Zingu/p/11821176.html