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

  • 相关阅读:
    MySQL------代码1024,can't get hostname for your address解决方法
    MySQL------存储过程的使用
    MyEclipse------如何添加jspsmartupload.jar,用于文件上传
    JQuery------实现鼠标点击和滑动不同效果
    CSS------如何让div中的div处于右下角
    JQuery------制作div模态框
    CSS------Filter属性的使用方法
    python使用元类
    python __new__()分析
    centos自带python2.6升级到python2.7。并解决yum pip easy_install pip等模块兼容性问题
  • 原文地址:https://www.cnblogs.com/wuhuisheng/p/1799635.html
Copyright © 2011-2022 走看看