请向我解释一下这个背景工作人员活动
本文关键字:一下 背景 活动 工作人员 解释 | 更新日期: 2023-09-27 17:54:23
我正在试用BackgroundWorker,并试图在每个事件中通知我的主线程。这些事情对我来说都是新的,我想问一下,我做得好吗?
我用以下方式简化了我的winforms问题:(它只有1个按钮,当我按下它时,在另一个线程中计数为10(
public partial class Form1 : Form
{
public void Subscribe(CountToTen c)
{
c.HandleWorkerEvent += new CountToTen.WorkerHandler(OtherThreadFinished);
}
private void OtherThreadFinished(CountToTen c, EventArgs e)
{
Debug.WriteLine("I'm ready !!!");
}
public Form1()
{
InitializeComponent();
}
private void btn_do_Click(object sender, EventArgs e)
{
CountToTen newThread = new CountToTen();
Subscribe(newThread);
newThread.StartCountingAndReportIfFinished();
}
}
CountToTen类:
public class CountToTen
{
public event WorkerHandler HandleWorkerEvent;
public EventArgs e;
public delegate void WorkerHandler(CountToTen c, EventArgs e);
public void StartCountingAndReportIfFinished()
{
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += delegate(object s, DoWorkEventArgs args)
{
for (int i = 1; i <= 10; i++)
{
Thread.Sleep(300);
Debug.WriteLine("Counting :" + i.ToString());
}
};
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(delegate(object o, RunWorkerCompletedEventArgs args)
{
Debug.WriteLine("Fromt Thread2 : I am finished!");
if (HandleWorkerEvent != null)
{
HandleWorkerEvent(this, e);
}
});
worker.RunWorkerAsync();
worker.Dispose();
}
}
当BW完成时,我正试图创建一个事件,并以我的主表单订阅该事件。它工作得很好,但是,我真的不明白这一行发生了什么:
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(delegate(object o, RunWorkerCompletedEventArgs args)
{
Debug.WriteLine("Fromt Thread2 : I am finished!");
if (HandleWorkerEvent != null)
{
HandleWorkerEvent(this, e);
}
});
当BW完成时,我不是在这里为它创建一个事件,然后为主线程调用另一个事件吗?这不是矫枉过正吗?我也可以直接订阅RunWorkerCompleteEventHandler吗?
我在这里有点困惑,请启蒙一个初学者。感谢
这是对BackgroundWorker的RunWorkerCompleted事件的订阅。您可以使用匿名方法或命名方法来完成此操作。您还可以通过以下方式删除委托类型声明:
worker.RunWorkerCompleted += (o, args) =>
{
// raise HandleWorkerEvent if there is any subscriber exists
if (HandleWorkerEvent != null)
HandleWorkerEvent(this, e);
worker.Dispose();
};
使用命名方法,这将看起来像
worker.RunWorkerCompleted += Worker_RunWorkerCompleted;
经办人:
void Worker_RunWorkerCompleted(object o, RunWorkerCompletedEventArgs args)
{
if (HandleWorkerEvent != null)
HandleWorkerEvent(this, e);
((BackgroundWorker)o).Dispose();
}
顺便说一句,我觉得上课更好,因为只有倒数十的逻辑。在表单上创建BackgroundWorker,并在DoWork事件处理程序中使用此类。表单还将处理其所有组件。
Am I not creating an event here for my BW, when it is finished,
and then call the another for the main thread?
是的。。您正在处理CountToTen类中BackgroundWorker的事件。显然,根据您的类架构,通过Event通知主窗体的唯一方法。
Is it not overkill? Could I subscribe directly to the RunWorkerCompleteEventHandler
as well?
你当然可以。。您的BackgroundWorker不会直接暴露给主窗体,否则您可以从主窗体本身订阅RunWorkerCompletedEvent。
注意:您不必调用Dispose((它通过Component实现IDisposable接口。有关详细信息,请参阅此处
更新
替代方式
public partial class Form1 : Form { private void btn_do_Click(object sender, EventArgs e) { CountToTen obj= new CountToTen(); obj.bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted); obj.bw.RunWorkerAsync(); } void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e){} } class CountToTen { public BackgroundWorker bw = new BackgroundWorker(); public CountToTen() { bw.DoWork += new DoWorkEventHandler(bw_DoWork); } void bw_DoWork(object sender, DoWorkEventArgs e) { //Do your Stuff } }
您可以直接订阅BackgroundWorker的RunWorkerCompleted事件,但这取决于您的业务逻辑和架构。