65.9K
CodeProject is changing. Read more.
Home

Debugging Cross thread exceptions

starIconstarIconstarIconemptyStarIconemptyStarIcon

3.00/5 (2 votes)

Aug 5, 2011

CPOL
viewsIcon

18686

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