我在这里做错了什么?
我的计划是从不同的线程更改标签文本属性,而不会出现“跨线程操作无效”异常。
private void button1_Click(object sender, EventArgs e)
{
Thread thread1 = new Thread(new ThreadStart(ChangeTime));
thread1.Start();
}
delegate void SetTimeDelegate();
private void ChangeTime()
{
while (true)
{
if (lbl1.InvokeRequired)
{
SetTimeDelegate setTime = new SetTimeDelegate(ChangeTime);
lbl1.Invoke(setTime);
}
else
{
lbl1.Text = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss tt");
Thread.Sleep(1000);
}
}
}