winform 使用委托 实现多线程访问控件

 

  private delegate void FlushClient();//代理
        private void ThreadFunction()
        {
            if (this.textBox1.InvokeRequired)
            {
                FlushClient fc = new FlushClient(ThreadFunction);
                this.Invoke(fc);
            }
            else
            {
                string s = DateTime.Now.ToString("yyyy年MM月dd日 HH时mm分ss秒");
                this.textBox1.Text = s;
               // this.label1.Text = s;
            }
        }

        private void CrossThreadFulush()
        {
            while (true)
            {
                Thread.Sleep(1000);
                ThreadFunction();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Thread th = new Thread(CrossThreadFulush);
            th.IsBackground = true;
            th.Start();
        }

点击button1执行结果:

 

posted @ 2013-11-18 09:58  JasonGu0  阅读(417)  评论(0编辑  收藏  举报