首页 > 代码库 > C#中跨线程的调用的方法--this.invoke

C#中跨线程的调用的方法--this.invoke

  private void button1_Click(object sender, EventArgs e)
        {
            Thread thread = new Thread(new ThreadStart(display));
            thread.Start();
        }
 
        private void display()
        {
            while (true)
            {
                 Thread.Sleep(100);
                 this.Invoke(new threadcall(SetText2));
            }
         }
 
        int count = 0;
 
        public delegate void threadcall();
 
        //threadcall Set = ;
 
        private void SetText2()
        {
            count++;
            this.label1.Text = count.ToString();
        }
 
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Dispose();
            //thread.Abort();
        }
    }
}