Keeping application responsive with BackgroundWorker



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 -


.



Relevant Pages

  • Re: Load secondary form in backgroundworker thread
    ... Is it possible to load this form in a backgroundworker and then ... IMHO, the correct design is to use a BackgroundWorker instance to run the code that initializes the data for the form, and then when that initialization is done, create the form on the main thread. ... A window is owned by the thread on which it was created. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: xorg 7.1.1 upgrade problem (More Info)
    ... X Protocol Version 11, Revision 0, Release 7.1.1 ... Before reporting problems, check http://wiki.x.org ... The following window managers were installed after the Xorg 7.1 upgrade, ... Failed to open file ...
    (Debian-User)
  • Re: Report progess during black box process
    ... Create the DataSource in a BackGroundWorker and make sure the DataSource is ... afterwards sort it in the BackgroundWorker. ... I am reporting to the user the progress while rows are added, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Load secondary form in backgroundworker thread
    ... Is it possible to load this form in a backgroundworker and then ... initialization is done, create the form on the main thread. ... A window is owned by the thread on which it was created. ... A very simple synchronization method would be to use the Control.Invoke ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: New HIDE (HLA IDE) version is available
    ... > that is not allowed in the dbg window ?? ... > Just reporting. ... labeled homepath which will probably look something like: ...
    (alt.lang.asm)