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 1977
刺人心
刺人心 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: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.

提交回复
热议问题