zoukankan      html  css  js  c++  java
  • 两个按钮分别是线程的打开和终止

    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;
                  }
            }
    }

  • 相关阅读:
    电商第一季函数笔记(1)
    沈逸老师PHP魔鬼特训笔记(2)
    PHP读书笔记(3)-常量
    2015/8/9 到家了,学完了CodeCademy的Python
    2015/8/4 告别飞思卡尔,抛下包袱上路
    2015/6/23 浪潮过去,我才来
    方维团购系统二次开发,项目经验
    方维团购系统整合云短信网短信平台,方维系统整合短信平台
    方维团购系统,下订单保存多个收货地址
    PHP木马查杀文件,木马查杀插件
  • 原文地址:https://www.cnblogs.com/wuhuisheng/p/1799635.html
Copyright © 2011-2022 走看看