In a backgroundworkder thread in the dowork event I can retrieve a row
(item) of data from a listbox (in a for loop). But if I try to retrieve
the same data from a listview I get a crossthread error.
Trying to access a ListBox instance from a thread other than where that
ListBox instance was created should also cause the cross-thread
exception. If it doesn't, that's a bug in .NET.
Is there a
workaround for this besides placing my for loop outside of the
backgroundworker (which means I would be calling the backgroundwoker
thousands of times) ?
Cross-thread exceptions are generally addressed using the Control.Invoke()
or Control.BeginInvoke() method. You should use that whether you are
accessing a ListView or ListBox object from your BackgroundWorker.DoWork
event handler.
Unless you post a concise-but-complete code example that demonstrates
exactly your problem, that's about as specific an answer as is worth
writing.
I will point out however that generally the flow of information is _from_
the BackgroundWorker thread to the main GUI thread, and not the other
way. If you are pulling data _from_ a GUI object during the actual
processing within your BackgroundWorker thread, it's likely there's a more
fundamental design flaw going on. Again, without actual code to comment
on, it's impossible to say for sure. But the general description you've
given suggests a need to fix the basic design, rather than to specifically
address the cross-thread exception issue.
Pete