小弟想用C#写个用来记时的秒表程序,面板上用5个label来分别显示 分:秒:毫秒,然后有一个按钮来启动和停止已经设置好的timer。我觉得基本的算法应该没问题,但是调试的时候程序停在“label5.Text = "0" + intMSecond.ToString();”处,提示错误为:“线程间操作无效。从不是创建“label5”的线程访问它。”而且前一行代码“intMSecond ++;”好象都还没有执行就提示这个错误了。小弟百思不的其解,着急死了,盼望各位高手给我指点以下问题在哪?如何改一下才对?感激不尽!!
代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Timers;
using System.Text;
using System.Windows.Forms;namespace mytimer1
{
    public partial class Form1 : Form
    {
        private int intMinute = 0, intSecond = 0, intMSecond = 0;
        private bool bolMStoSECOND = false, bolSECONDtoMINUTE = false;
        System.Timers.Timer timerObj;
        public Form1()
        {
            InitializeComponent();
        }        private void button1_Click(object sender, EventArgs e)
        {
            timerObj.Start();
        }        private void Form1_Load(object sender, EventArgs e)
        {
            
            // 创建timer对象,设置它的Elapsed事件处理程序
            timerObj = new System.Timers.Timer(1000);
            ElapsedEventHandler eeh = new ElapsedEventHandler(clockhandle);
            timerObj.Elapsed += eeh;
        }
                // Elapsed事件的处理程序
        private void clockhandle(object sender, ElapsedEventArgs e)
        {
            
            intMSecond ++;
            if (intMSecond < 10)
                label5.Text = "0" + intMSecond.ToString();   !!!调试时问题就出在这行,好象说是跨线程访问windows窗体控件失败。
            
            else
            {
                if (intMSecond == 100)
                {
                    intMSecond = 0;
                    intSecond++;
                    bolMStoSECOND = true;
                    label5.Text = "00";
                }
                else
                    label5.Text = intMSecond.ToString();
            }
            if (bolMStoSECOND == true)
            {
                if (intSecond < 10)
                {
                    label3.Text = "0" + intSecond.ToString();
                    bolMStoSECOND = false;
                }
                else
                {
                    if (intSecond == 60)
                    {
                        intSecond = 0;
                        intMinute++;
                        bolSECONDtoMINUTE = true;
                        bolMStoSECOND = false;
                        label3.Text = "00";
                    }
                    else
                    {
                        label3.Text = intSecond.ToString();
                        bolMStoSECOND = false;
                    }
                }
            }
            if (bolSECONDtoMINUTE == true)
            {
                if (intMinute < 10)
                {
                    label1.Text = "0" + intMinute.ToString();
                    bolSECONDtoMINUTE = false;
                }
                else
                {
                    if (intMinute == 60)
                    {
                        intMinute = 0;
                        intMSecond = 0;
                        intSecond = 0;
                        bolSECONDtoMINUTE = false;
                        label1.Text = "00";
                        label3.Text = "00";
                        label5.Text = "00";
                        timerObj.Stop();
                    }
                    else
                    {
                        label1.Text = intSecond.ToString();
                        bolSECONDtoMINUTE = false;
                    }
                }
            }
            
                  
        }
    }
}

解决方案 »

  1.   

    VS2003里clockhandle运行正确 没有出现你说的异常
    不过timerObj = new System.Timers.Timer(1000);
    这里1000应改为10吧没有2005 而且你的代码也不全 重现不了你的情况
      

  2.   

    线程中不能直接操纵UI,参看
    http://blog.csdn.net/Knight94/archive/2006/08/24/1111267.aspx你需要通过Invoke或者BeginInvoke去修改控件
      

  3.   

    没有注意过timer是否属于多线程!!关注 !
      

  4.   

    05里面增强了THREAD操作安全性,所以会报错。03 下不会。
    变通一下就可以了。