What am I doing wrong here?
my plan is to change the label text property from a different thread without getting the "Cross-thread operation not valid" exception.
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);
}
}
}