So far I have seen 1000 examples how to set items in listbox objects from a different thread using the Invoke method.
However, I cannot find any solution on how to simply read the items from a listbox from a background worker tread. How do I invoke this before reading the SelectedItems for example…
foreach (var item in CheckedListBox1.SelectedItems)
{
//Do something
}
Above code running in the background worker generated the following error:
Cross-thread operation not valid: Control ‘CheckedListBox1’ accessed from a thread other than the thread it was created on.
You may similarly use
Invoketo read UI elements from a background thread:If you know the type of your items, you may specify the type in the
Castcall, and return anIList<YourType>rather than the non-genericIList.