我有一个分层的工人阶级,我正试图从中获取进度报告。我所拥有的看起来像这样:
public class Form1
{
private void Start_Click()
{
Controller controller = new Controller();
controller.RunProcess();
}
}
public class Controller
{
public void RunProcess()
{
Thread newThread = new Thread(new ThreadStart(DoEverything));
newThread.Start();
}
private void DoEverything()
{
// Commencing operation...
Class1 class1 = new Class1();
class1.DoStuff();
Class2 class2 = new Class2();
class2.DoMoreStuff();
}
}
public class Class1
{
public void DoStuff()
{
// Doing stuff
Thread.Sleep(1000);
// Want to report progress here
}
}
public class Class2
{
public void DoMoreStuff()
{
// Doing more stuff
Thread.Sleep(2000);
// Want to report progress here as well
}
}
我以前使用过 BackgroundWorker 类,但我认为我需要一些更自由的形式来完成这样的事情。我想我可以使用委托/事件解决方案,但我不确定如何在这里应用它。假设我在 Form1 上有一些标签或其他东西,我希望能够使用 class1 和 class2 的进度进行更新,那么最好的方法是什么?