C# should I create one Background worker or many?

邮差的信 提交于 2019-12-31 08:45:12

问题


I am one of those accidental programmer so I don't have that much knowledge regarding programming best practice.

I have an application that currently uses 4 Background Worker.

So I declare them:

private BackgroundWorker bw1;
private BackgroundWorker bw2;
private BackgroundWorker bw3;
private BackgroundWorker bw4;

Then configure them:

bw1 = new BackgroundWorker();
bw1.WorkerReportsProgress = true;
bw1.DoWork += new DoWorkEventHandler(bw1_DoWork);
bw1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw1_RunWorkerCompleted);
bw1.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);

bw2 = new BackgroundWorker();
bw2.WorkerReportsProgress = true;
bw2.DoWork += new DoWorkEventHandler(bw2_DoWork);
bw2.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw2_RunWorkerCompleted);
bw2.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);

bw3 = new BackgroundWorker();
bw3.WorkerReportsProgress = true;
bw3.DoWork += new DoWorkEventHandler(bw3_DoWork);
bw3.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw3_RunWorkerCompleted);
bw3.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);

bw4 = new BackgroundWorker();
bw4.WorkerReportsProgress = true;
bw4.DoWork += new DoWorkEventHandler(bw4_DoWork);
bw4.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw4_RunWorkerCompleted);
bw4.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);

And then I use the bw1.RunWorkerAsync(), bw2.RunWorkerAsync(), and so on...

The thing is I never call them simultaneously, they are call at different point in a pretty linear way.

So my question is, is it better to have many "pre-configured" background workers or to have one and change the DoWork and RunWorkerCompleted events according to what I want it to do?


回答1:


From an architectural point of view, it is best to have a separate background worker for each background task, that is logically unrelated to another tasks of the class.




回答2:


Usually I use background workers in a quite different pattern. Instead of defining them all at once in the beginning including their respective event handlers, I create them on the fly when I do something that needs them.

public void SomeEventHandlerMaybe(object sender, EventArgs e) {
  // do something

  var bw = new BackgroundWorker();
  bw.ReportsProgress = true;
  bw.DoWork += delegate {
    // do work. You can use locals from here
  };
  bw.ProgressChanged += delegate { ... };
  bw.RunWorkerCompleted += delegate {
    // do something with the results.
  };
  bw.RunWorkerAsync();
}

Something like that. It has the benefit that you have all code that does something with or in the background worker in one place and roughly in the right order as well.




回答3:


Generally it's reasonable to use multiple threads if it helps with more efficient resource usage on your system. For CPU intensive tasks, one thread per CPU core is a good starting point. For IO intensive tasks, you can certainly have many more than that.

If you have the flexibility to use .NET 4, I would look into the Task Parallel Library instead of BackgroundWorker. By default, it will make relatively smart decisions for you about how many threads to run concurrently in addition to providing an easier programming model.




回答4:


Doing your processing using BackgroundWorker means you do it in a seperate thread. So if you don't need multithreading, there's no need for seperate background workers. If you wait for completion after you start each of your workers, you can have just one worker. It may also may remove some of the code duplication...




回答5:


You run workers for some computational purpose. If the calculation of one call is used or depends in any way to another, you can use more then one worker, in order to achieve better performance (by the way this is too a subject for measuring). If you just need 4 jobs done, and run them in separate thread just for not block the main UI, one worker is pretty good solution.



来源:https://stackoverflow.com/questions/7406895/c-sharp-should-i-create-one-background-worker-or-many

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!