Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Background Worker C# winform

is it a bad idea to load everything in from the background worker?? Current code is Executed on Form_load. we are pulling a lot of data from webservice. some long running works are in background worker.

would it be a bad idea to load everything from background worker no matter how small or big the code is?? every function to run in background worker? or is this going to make this code messy and treading nightmare.

Thank you.

like image 522
RedPanda Avatar asked Jul 01 '10 23:07

RedPanda


3 Answers

The size of the code is not a metric you should use to determine whether to perform work on a separate thread. Worst case length of execution is.

First, there aren't many UI designs where firing off a bunch of work on Form_Load is really desirable. If it's possible, I typically either:

  • Initiate that work prior to opening the form directly in response to a user action.
  • Move all of the work into the background and asynchronously update (bind?) the results to the form.
  • Delay the work until the user specifically performs some action which requires it.

Your users will want/expect your forms to be extremely fast and responsive regardless of how much work is being done. Executing potentially long-running operations during Form_Load is always going to result in a bad user experience.

BackgroundWorker is only one mechanism for performing work asynchronously, there are many others. You should choose the one that is most appropriate for each situation.

like image 52
hemp Avatar answered Nov 08 '22 00:11

hemp


BackgroundWorker is normally used to make a Winforms UI more responsive. You can use it for background processing in a web application, but I don't; I use a ThreadPool in a Windows Service.

like image 1
Robert Harvey Avatar answered Nov 08 '22 00:11

Robert Harvey


Its good to stick potentailly long-running code into a background worker so that your application remains responsive, but there is no need to put everything as a background worker.

If your code calls a web service (or any sort of external system that might time out), or does something that it likely to take longer than a few seconds, then that would be a good candidate for putting into a background worker, however if anything consistently takes less than a second or so to execute I proabably wouldn't bother.

like image 1
Justin Avatar answered Nov 07 '22 22:11

Justin