Error: Cross-thread operation not valid - Using Multi-Threading
Posted on 2007-08-02
I am trying to add an item to a listbox. The lst.Items.Add is being performed within a custom event on the form. I am getting the error:
Cross-thread operation not valid: Control 'lstProcessed' accessed from a thread other than the thread it was created on.
I am probably about the densest person around when it comes to understanding multithreading. However, there is reference in the error and MS help about using CheckForIllegalCrossThreadCalls. When I add it I get the warning:
Access of shared member, constant member, enum member or nested type through an instance; qualifying expression will not be evaluated.
but it seems to work.
My questions: what are the ramifications of using this property? Will the warning lead to any serious consequenses down the road? Where can I find information that can be rather easily digested (I am cramming TOO much into my head right now to try to absorb anything too deep) on using multi-threading?
This is my 3rd question relating to one project I am working on. I have a feeling it probably should be multi-threaded but have no idea how to do it.
I have a form that is creating multiple instances of a class that can raise events back to the form. Each class has different values set on properties that basically set up a FileSystemWatcher. I watch several different directories and different filters on each one. When the FSW files in the class, it processes the file, then raises an event back to the form that basically just adds an item to a listbox giving the path, filename and status of the processed file. The class then goes back to watching the directory.