I am trying to update a Label in a Thread. I have tried multiple different methods and still can't update my labels text here is my code
public form1()
{
InitializeComponent();
new Thread(SampleFunction).Start();
}
private void setLabel1TextSafe(string txt)
{
if (label1.InvokeRequired)
{
label1.Invoke(new Action(() => label1.Text = txt));
return;
}
label1.Text = txt;
}
public void AppendText(string value)
{
label1.Text += value;
}
void SampleFunction()
{
// Gets executed on a seperate thread and
// doesn't block the UI while sleeping
for (int i = 0; i < 5; i++)
{
setLabel1TextSafe("Heya");
Thread.Sleep(1000);
}
}
There is no error being cast but the label text is not being changed or updated Anyone can help? Thank You