private void button1_Click(object sender, EventArgs e)
        {
            //带参数
            ThreadParam param = new ThreadParam();
            param.t_i = 3;  //定义循环次数
            Thread _WriteThread = new Thread(new ParameterizedThreadStart(doThread));   
            _WriteThread.Start(param);   //启动新线程,不占用主界面线程时间
            textBox1.Text = "演示";
        }

        private void doThread(object threadData)
        {
            ThreadParam param = (ThreadParam)threadData;
            for (int i = 0; i < param.t_i; i++)
            {
                //循环体,例如暂停1s,会看到效果,先显示“演示”后显示循环次数
                System.Threading.Thread.Sleep(1000);
            }
            //循环完毕后赋值
           if (this.IsDisposed) return;
           this.Invoke(new EventHandler(delegate(object s1, EventArgs e1)
           {
               textBox1.Text = param.t_i.ToString();

           }));
 

        }

        private void button2_Click(object sender, EventArgs e)
        {
            //不带参数
            Thread _WriteThread = new Thread(new ThreadStart(doThreadN));
            _WriteThread.Start();   //启动新线程,不占用主界面线程时间
            textBox1.Text = "演示";
        }

        private void doThreadN()
        {
           
            for (int i = 0; i < 3; i++)
            {
                //循环体,例如暂停1s,会看到效果,先显示“演示”后显示循环次数
                System.Threading.Thread.Sleep(1000);
            }
            //循环完毕后赋值
            if (this.IsDisposed) return;
            this.Invoke(new EventHandler(delegate(object s1, EventArgs e1)
            {
                textBox1.Text = "3";

            }));


        }

    }

     class ThreadParam
    {  
        public int t_i;
     
    }

  

posted on 2014-12-11 17:31  AmyAlisa  阅读(234)  评论(0编辑  收藏  举报