Debugging Cross thread exceptions





3.00/5 (2 votes)
Recently, I was stuck with following error,
"Cross-thread operation not valid: Control 'label1' accessed from a thread other than the thread it was created on."
The error has been resolved using below code. I am posting it here because it may help someone.
public void SetStatus(string msg)
{
if (label1.InvokeRequired)
label1.Invoke(new MethodInvoker(delegate
{
label1.Text = msg;
}));
else
label1.Text = msg;
}
Credits goes to 'IAbstract'. Link :Click here