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

  • 相关阅读:
    一步一步学数据结构之(动态申请二维数组)
    运维自动化
    oracle查看登录到oracle服务器的客户端ip
    权限
    windows下使用SQLPLUS制作BAT执行SQL文件
    rsyslog传输type
    C经典实例
    mysql导出数据库数据及表结构
    解决oracle11g无法导出空表问题
    opennebula onenebula
  • 原文地址:https://www.cnblogs.com/wuhuisheng/p/1799635.html
Copyright © 2011-2022 走看看