Why am I getting this error:“Cross-thread operation not valid: Control lbFolders accessed from a thread other than the thread it was created on.”?

后端 未结 7 1974
刺人心
刺人心 2020-11-29 12:49

This is baffling me, maybe somebody can shine the light of education on my ignorance. This is in a C# windows app. I am accessing the contents of a listbox from a thread.

相关标签:
7条回答
  • 2020-11-29 13:37

    Because you created a control in a thread and you're trying to reach it from another one. Call the InvokeRequired property as shown here:

    private void RunMe()
    {
        if (!InvokeRequired)
        {
            myLabel.Text = "You pushed the button!";
        }
        else
        {
            Invoke(new ThreadStart(RunMe));
        }
    }
    
    0 讨论(0)
提交回复
热议问题