请向我解释这个BackgroundWorker活动

时间:2013-01-18 11:33:00

标签: c# winforms

我正在尝试使用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吗?

我在这里有点困惑,请指教初学者。 感谢

2 个答案:

答案 0 :(得分:0)

您可以直接订阅BackgroundWorker的RunWorkerCompleted事件,但这取决于您的业务逻辑和架构。

答案 1 :(得分:0)

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通知你的mainform的唯一方法。

  Is it not overkill? Could I subscribe directly to the RunWorkerCompleteEventHandler
  as well? 

当然你可以..你的BackgroundWorker没有直接暴露给mainform,否则你可以从那里订阅RunWorkerCompletedEvent。

注意:  您不必调用Dispose()。它通过Component实现IDisposable接口。  有关详细信息see here

更新

  • 替代方式

    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
    }
    }