Click here to Skip to main content
15,924,482 members
Articles / Programming Languages / C#
Tip/Trick

Debugging Cross thread exceptions

Rate me:
Please Sign up or sign in to vote.
3.00/5 (2 votes)
5 Aug 2011CPOL 18.2K   3   2
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.
C#
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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
A Software programmer. Works on ASP.NET, MVC, VB.NET, C#, .NET Compact framework, SSIS, SQL Server, PHP and its frameworks. Other than that likes to play games in PC.

Comments and Discussions

 
GeneralReason for my vote of 5 While it may not be debugging, I am ... Pin
Jess Toney23-Sep-11 2:17
Jess Toney23-Sep-11 2:17 
GeneralReason for my vote of 1 sorry for downvoting, but on CP ther... Pin
johannesnestler11-Aug-11 3:17
johannesnestler11-Aug-11 3:17 
Reason for my vote of 1
sorry for downvoting, but on CP there are SOOOO many good articles about this topic and this is one of the most asked questions too... Sorry this is not real a "Tip" and has nothing to do with "debugging" cross thread exceptions...

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.