| 本帖最后由 _xixi_ 于 2014-7-14 14:24 编辑 
 总算把委托的方法给实现了,谢谢各位的指点!
 
 复制代码namespace clock
{
    delegate void GetSec();
    public partial class Form1 : Form
    {
        GetSec getSec;
        Thread th;
        public Form1()
        {
            InitializeComponent();
        }
        private void btnRun_Click(object sender, EventArgs e)
        {
            if (th==null||!th.IsAlive)
            {
                th = new Thread(new ThreadStart(SecondRun));
                th.Start();
            }
        }
        private void GetSecond()
        {
            if (txtSecond.InvokeRequired == true) //判断是否跨线程访问了
            {
                    txtSecond.Invoke(getSec);   //执行委托,也就是回到主线程执行GetSec()方法
            }
            else
            {  
                if (th.IsAlive)
                {
                    int s = Convert.ToInt32(txtSecond.Text);
                    if (s == 59)
                    {
                        txtSecond.Text = "0";
                    }
                    else
                    {
                        s += 1;
                        txtSecond.Text = s.ToString();
                    }
                }
                Thread.Sleep(10);
            }
            
        }
        private void SecondRun()
        {
            getSec = new GetSec(GetSecond);
            while (true)
            {
                getSec();
            }
           
        }
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (th.IsAlive)
            {
                th.Abort();//确保关闭窗口之前,终止线程
            }
        }
    }
}
 
 
 |