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 1973
刺人心
刺人心 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:20

    You can't access GUI elements from a separate thread. Use a delegate to make the change.

    eg.

    lblStatus.Invoke((Action)(() => lblStatus.Text = counter.ToString()));
    

    or older skool:

    lblTest.Invoke((MethodInvoker)(delegate() 
    { 
      lblTest.Text = i.ToString(); 
    }));
    

    I've got a blog post on how to do this in all the .Net releases here.

    0 讨论(0)
  • 2020-11-29 13:21
    prgAll.Maximum = lbFolders.SelectedItems.Count;
    

    On that line you perform an assignment (set/add), which by default is not thread-safe.

    On the second line it's just a get operation, where thread-safety merely doesn't matter.

    EDIT: I don't mean access to the prgAll element.

    Accessing the Count property changes the internal state of the ListBox inner collection, that is why it throws the exception.

    0 讨论(0)
  • 2020-11-29 13:26

    You're trying to write to a control from a thread other than the main thread. Use Invoke or BeginInvoke.

    void SetMax()
    {
        if (prgAll.InvokeRequired)
        {
            prgAll.BeginInvoke(new MethodInvoker(SetMax));
            return;
        }
    
        prgAll.Maximum = lbFolders.SelectedItems.Count;
    }
    
    0 讨论(0)
  • 2020-11-29 13:26

    Try this:

    private delegate void xThreadCallBack();
    private void ThreadCallBack()
    {
        if (this.InvokeRequired)
        {
            this.BeginInvoke(new xThreadCallBack(ThreadCallBack));
        }
        else
        {
            //do what you want
        }
    }
    

    Though, the answer with the lambda expression would suffice.

    0 讨论(0)
  • 2020-11-29 13:30

    You can't touch a GUI object from a thread that isn't the main GUI thread. See here for more details and the solution.

    0 讨论(0)
  • 2020-11-29 13:32

    The Count property of SelectedItems is not thread-safe, so you can't use it cross-thread.

    0 讨论(0)
提交回复
热议问题