I have a UI that needs a couple of threads to do some significant processing
on a couple of different forms - and while it's at it, update the UI (set
textboxes, fill in listviews). I created a base class for the worker class,
and made up some functions/delegates to handle the invoke stuff for the UI
and that was fine for a prototype. I rewrote this chunk, broke things out
into different classes - but the threading is still the same - and one
specific problem I'm having is this.
Within the worker thread, I do this:
ListViewItem lvwItem = new ListViewItem();
I want to try to do as much work as possible, while leaving a lot of
potential functionality in the worker thread. So I'd like to pre-populate a
ListViewItem - and then do the Invoke to just add that pre-filled
ListViewItem to the listview on the UI.
On that line, I get an error of:
Illegal cross-thread operation: Control 'frmPopup' accessed from a thread
other than the thread it was created on.
Stack trace where the illegal operation occurred was:
at System.Windows.Forms.Control.get_Handle() etc, etc
And it happens right at that line above when I'm trying to create a
ListViewItem programatically. I am assuming that when you create a UI
element programatically, it needs a window handle? If so - how do I get
around this? And I know I've had this work before, the only thing different
is I have this functionality in a seperate class.
Also - I got rid of 100% of the functionality in that thread (commented it
out) - and slowly added lines back in, and this is the only, even remote
reference to a UI element in this chunk of code, and this is the specific
line that throws the exception.
Any ideas???