利用线程让label显示当前时间
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        string s;
        public Form1(string s1)
        {
            s = s1;
        }
        public void showtime()
        {            s = DateTime.Now.ToLongTimeString();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Form1 f = new Form1(label1.Text);
            Thread th = new Thread(new ThreadStart(f.showtime));
            th.Start();
            th.Join();
        }
    }
}点了按钮 label没反应 求解 求解 在线等!!!

解决方案 »

  1.   

    public partial class Form1 : Form
        {
            private delegate void FlushClient();
            public Form1()
            {
                InitializeComponent();
            }
            
            private void button1_Click(object sender, EventArgs e)
            {            Thread th = new Thread(new ThreadStart(ThreadFunction));
                th.Start();
                
            }
           
            private void ThreadFunction()
            {
                if (this.label1.InvokeRequired)//等待异步
                {
                    FlushClient fc = new FlushClient(ThreadFunction);
                    this.Invoke(fc);//通过代理调用刷新方法
                }
                else
                {
                   
                    this.label1.Text = DateTime.Now.ToString();
                }
            }
        }
      

  2.   

    你的线程干什么Join()呢?Join就挂起了。另外,为了保证UI的线程安全,必须用Delegate,Invoke一下。
      

  3.   

    Timer timer1;
      this.timer1.Interval = 1000;
      this.timer1.Tick += new System.EventHandler(this.timer1_Tick);  private void timer1_Tick(object sender, EventArgs e)
      {
      timebox.Text = DateTime.Now.ToString();   
      }