c#:“跨线程操作无效”

时间:2016-04-12 07:26:16

标签: c# multithreading

public partial class Form1 : Form
{
    System.Timers.Timer aTimer;
    double score, multiplier = 1;
    int numberOf1 = 0;
    public Form1()
    {
        InitializeComponent();
        aTimer = new System.Timers.Timer();
        aTimer.Interval = 1000;
        aTimer.Elapsed += ATimer_Elapsed;
        aTimer.Start();            
    }

    private void ATimer_Elapsed(object sender, ElapsedEventArgs e)
    {
        score += numberOf1 * 5;
        label1.Text = score.ToString();
    }

    private void label1_Click(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
        numberOf1 += 1;

    }
}

当我运行它时,会突出显示label1.Text = score.ToString();并给我一个错误说“交叉线程操作无效”。

我不明白为什么它不起作用,我尝试用谷歌搜索它,但我只找到了我无法复制的具体例子。

0 个答案:

没有答案