本文介绍了C#我应该创建一个后台工作还是很多?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我是属于意外程序员,所以我没有那么多的知识对于编程的最佳实践之一。 我目前使用4后台工作的应用程序。 所以我声明它们: 私人BackgroundWorker的BW1; 私人BackgroundWorker的BW2; 私人BackgroundWorker的BW3; 私人BackgroundWorker的BW4; 将它们配置: BW1 =新的BackgroundWorker(); bw1.WorkerReportsProgress = TRUE; bw1.DoWork + =新DoWorkEventHandler(bw1_DoWork); bw1.RunWorkerCompleted + =新RunWorkerCompletedEventHandler(bw1_RunWorkerCompleted); bw1.ProgressChanged + =新ProgressChangedEventHandler(bw_ProgressChanged); BW2 =新的BackgroundWorker(); bw2.WorkerReportsProgress = TRUE; bw2.DoWork + =新DoWorkEventHandler(bw2_DoWork); bw2.RunWorkerCompleted + =新RunWorkerCompletedEventHandler(bw2_RunWorkerCompleted); bw2.ProgressChanged + =新ProgressChangedEventHandler(bw_ProgressChanged); BW3 =新的BackgroundWorker(); bw3.WorkerReportsProgress = TRUE; bw3.DoWork + =新DoWorkEventHandler(bw3_DoWork); bw3.RunWorkerCompleted + =新RunWorkerCompletedEventHandler(bw3_RunWorkerCompleted); bw3.ProgressChanged + =新ProgressChangedEventHandler(bw_ProgressChanged); BW4 =新的BackgroundWorker(); bw4.WorkerReportsProgress = TRUE; bw4.DoWork + =新DoWorkEventHandler(bw4_DoWork); bw4.RunWorkerCompleted + =新RunWorkerCompletedEventHandler(bw4_RunWorkerCompleted); bw4.ProgressChanged + =新ProgressChangedEventHandler(bw_ProgressChanged); 然后我用bw1.RunWorkerAsync(),bw2.RunWorkerAsync(),等等。 .. 的事情是我从来没有同时调用他们,他们是在一个非常线性的方式不同点的呼叫。 所以我的问题是,是它更好地有许多预配置的背景工作者或有一个,并根据修改的DoWork和RunWorkerCompleted事件,我想要它做的? 解决方案 从建筑的角度来看,最好是为每个后台任务一个单独的后台工作,也就是逻辑无关的类的另一个任务。 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? 解决方案 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. 这篇关于C#我应该创建一个后台工作还是很多?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 06-09 16:15