多线程和委托简单例子

 private void btnRun_Click(object sender, EventArgs e)
        {
            Thread th = new Thread(new ThreadStart(heating));
            th.IsBackground = true;
            th.Start();
           
        }
        int templeture;
        delegate void DisplayHandler(string tempStr);
      
        protected void heating()
        {

            for (int i = 0; i < 100; i++)
            {
                templeture = i;
                string tp = i.ToString();
                //this.Invoke(new DisplayHandler(Displaytemp));//这里是不带参数
                this.BeginInvoke(new DisplayHandler(Displaytemp), tp);//带参数据
                Thread.Sleep(1000);

            }
        }
        protected void Displaytemp(string tempStr)
        {

            this.label1.Text = tempStr;// templeture.ToString();
        }

posted @ 2011-08-12 10:33  咸鱼公子  Views(297)  Comments(1)    收藏  举报