问题描述
我的Program.cs调用MDI父frmMain。 frmMain然后打开基于用户行为的不同子窗体。
My program.cs calls the mdi parent frmMain. frmMain then opens different child forms based on user action.
所有的处理逻辑被写入BusinessLogic.cs。 frmMain在加载调用BusinessLogic.cs的方法,初步填充数据。子窗体也叫BusinessLogic来获取数据并进行处理。我想要做的这一切,通过一个BackgroundWorker即frmMain调用BusinessLogic.cs的(比方说)StartBGWorker()方法,该方法创建一个BackgroundWorker这个特定的呼叫,来电Do_work事件,请问取和放大器;完成后处理和关闭。
All the processing logic is written in BusinessLogic.cs. frmMain at load calls the methods of BusinessLogic.cs to initially populate the data. The child forms too call BusinessLogic to fetch data and process it. I'd like to do all this through a BackGroundWorker ie the frmMain calls the (say) StartBGWorker() method of BusinessLogic.cs and this method creates a backgroundworker for this specific call, calls the Do_work event, does the fetching & processing and closes when done.
我在一个有关如何创建实例和BackgroundWorker的事件损失。那么究竟怎么做我在一个类文件中使用的BackgroundWorker?
I'm at a loss about how to create the instance and the events for backgroundworker. So how exactly do I use backgroundworker in a class file?
编辑:也许我的逻辑是错误的,我应该添加一个BGW给每个窗体调用BusinessLogig.cs。于是每当我打电话BusinessLogic的方法,我可以通过BackgroundWorker的做到这一点。这会是一个更好的实施?
Maybe my logic is wrong and I should add a BGW to each form that calls BusinessLogig.cs. Then whenever I call a method of BusinessLogic I can do so through backgroundworker. Would this be a better implementation?
EDIT2:感觉有点白痴的我的疑问,现在,我发现了一种方法。我刚刚创建了一个公共静态方法BGW在BusinessLogic初始化code。现在,每当我需要做的处理我先请从我的表单此方法。我想知道,如果我的实现BGW的是标准的或有什么办法改进设计。
Feel a bit idiotic about my doubt now that I found a way. I just created a public static method with the initialize code of BGW in BusinessLogic. Now whenever I need to do processing I first call this method from my forms.I'd like to know if my implementation of BGW is standard or is there any way to improve the design.
推荐答案
包括:
using System.ComponentModel;
将其定义在类:
Define it in your class:
private BackgroundWorker BackgroundWorker = new BackgroundWorker();
初始化:
Initialize it:
BackgroundWorker.WorkerSupportsCancellation = false;
BackgroundWorker.WorkerReportsProgress = true;
BackgroundWorker.DoWork += BackgroundWorker_DoWork;
BackgroundWorker.ProgressChanged += BackgroundWorker_ProgressChanged;
BackgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler( BackgroundWorker_RunWorkerCompleted );
要启动:
BackgroundWorker.RunWorkerAsync();
的用法是完全一样的,你知道,从Windows窗体。
The usage is exactly the same as you know from Windows Forms.
这篇关于如何在类文件中使用的BackgroundWorker?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!