日期:2014-05-18  浏览次数:20675 次

请给我一个用线程操作主窗体的最简单的源码吧
一个主窗体,一个按钮,一个LABEL
点按钮,建立一个线程,将当前时间在主窗体的label 显示。

用thread 或 backgroundworker

另外,如果要操作主窗体或一个DATATABLE,上边两个方法那个好?

------解决方案--------------------
C# code


        private void button1_Click(object sender, EventArgs e)
        {
            Thread thread = new Thread(new ThreadStart(ThreadTest));
            thread.Start();
        }

        private void ThreadTest()
        {
            UpdateTimeHandler ut = new UpdateTimeHandler(UpdateTime);
            this.Invoke(ut, DateTime.Now);
        }

        private void UpdateTime(DateTime dt)
        {
            this.label1.Text = dt.ToString();
        }

        private delegate void UpdateTimeHandler(DateTime dt);