J
Joseph Geretz
A subtle point: If your BackgroundWorker reports its progress back to the
main application window, you'll need to maintain a positive ratio of time
spent within the BackgroundWorker event (_DoWork) on the separate thread, vs
time spent in the report back (_ProgressChanged) event which operates on the
same thread as the main window. If this isn't the case, then you might as
well run your work in the main window thread and avoid all the thread
switching.
A practical example: I'm working on a file processor; it's reading a file
with over a million records. I placed the file processing code into a
BackgroundWorker thread in order to keep the application UI responsive. As
I'm developing this application, there's really nothing in the processing
loop aside from the read of the record, since I decided to get the
infrastructure in place first and then proceed to the details of processing
each record. On my first attempt, reporting status back with every record
locked up the application as tight as if the processing loop would have been
in the main window thread. Reporting back every 100th record wasn't any
better. It wasn't until I scaled back the feedback to every 1000th record
that I saw a positive improvement. (Of course, once I insert processing code
into my loop, this will increase the time spent in the worker thread and so
I'll probably end up with feedback on every 100th record by the time I'm
done.)
It's a subtle point and it took me a few minutes to figure out the
principle. I guess this is pretty simple, but I'm writing this up in case
there's anyone out there who can benefit form this advice.
Hope this helps,
- Joseph Geretz -
main application window, you'll need to maintain a positive ratio of time
spent within the BackgroundWorker event (_DoWork) on the separate thread, vs
time spent in the report back (_ProgressChanged) event which operates on the
same thread as the main window. If this isn't the case, then you might as
well run your work in the main window thread and avoid all the thread
switching.
A practical example: I'm working on a file processor; it's reading a file
with over a million records. I placed the file processing code into a
BackgroundWorker thread in order to keep the application UI responsive. As
I'm developing this application, there's really nothing in the processing
loop aside from the read of the record, since I decided to get the
infrastructure in place first and then proceed to the details of processing
each record. On my first attempt, reporting status back with every record
locked up the application as tight as if the processing loop would have been
in the main window thread. Reporting back every 100th record wasn't any
better. It wasn't until I scaled back the feedback to every 1000th record
that I saw a positive improvement. (Of course, once I insert processing code
into my loop, this will increase the time spent in the worker thread and so
I'll probably end up with feedback on every 100th record by the time I'm
done.)
It's a subtle point and it took me a few minutes to figure out the
principle. I guess this is pretty simple, but I'm writing this up in case
there's anyone out there who can benefit form this advice.
Hope this helps,
- Joseph Geretz -