• 两个按钮分别是线程的打开和终止


    public partial class Form1 : Form
        {
            Thread thread=null;
            int n = 0;
    bool severIsRun=false;//用来村示线程是否启动
    private delegate void myDelegate(string str);//定义委托
    private void setRich(string str)//委托
            {
                if (this.textBox1.InvokeRequired)
                {
                    myDelegate md = new myDelegate(this.setRich);
                    this.Invoke(md, new object[] { str });
                }
                else
                    this.textBox1.Text =str;
            }

            public Form1()
            {
                InitializeComponent();
            }

            private void run()
            {
                while(true)
                {
                    setRich((n++).toString());
                    Thread.Sleep(1000);
                }
            }

            private void button1_Click(object sender, EventArgs e)
            {
               if(!severIsRun)
               {
                ThreadStart ts =new ThreadStart(run);
                Thread = new Thread(ts);
                severIsRun=true;
                thread.Start();
               }
            }

            private void button2_Click(object sender, EventArgs e)
            {
               if(severIsRun)
               {
                        thread.Abort();
                         severIsRun=false;
                }
           }

            private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                if(severIsRun)
                 {
                        thread.Abort();
                         severIsRun=false;
                  }
            }
    }

  • 相关阅读:
    Business English E-mail
    向数据库更新数据
    遍历DataTable
    Convert SqlDataReader to DataTable
    (十七)map、flatMap和reduce方法的补充
    (4)六顶思考帽
    (3)学习方法思维导图
    (十六)call、apply、bind的实现以及区别
    (十五)关于继承
    (十四)防抖和节流
  • 原文地址:https://www.cnblogs.com/wuhuisheng/p/1799635.html
Copyright © 2020-2023  润新知