BackgroundWorker is a class that runs in a separate thread. So either way,
you're working on a separate thread from the UI thread, so you're better off
going with the BackgroundWorker functionality, which is designed to
integrate with the STA Windows Forms environment. The problem in either case
is marshalling to the UI thread.
When the BackgroundWorker class's thread calls a method, or does anything
else with the Form, it is doing it in the context of its' own separate
thread. So, the BackgroundWorker's DoWork method cannot call any methods or
touch anything in the UI thread. However, it CAN raise events, and in your
case, you want to call the ReportProgress(int, object) method. This passes a
ProgressChangedEventArgs instance to the UI thread in the event. The UI
thread can then call whatever methods or change whatever properties it needs
to from there.
The trick is to use the overload of the RunWorkerAsynch method that takes an
object as a parameter. You can pass data to the BackgroundWorker via this
parameter, and when it calls ReportProgress, it can pass the modified data
back. This enables the BackgroundWorker to "communicate" with the UI thread.
Here's an excellent example:
http://www.mikedub.net/mikeDubSampl...eallySimpleMultithreadingInWindowsForms20.htm
--
HTH,
Kevin Spencer
Microsoft MVP
Software Composer
http://unclechutney.blogspot.com
The shortest distance between 2 points is a curve.