BackgroundWorker不会在用户控件内部启动

时间:2016-06-25 14:52:16

标签: c#

我目前正在创建一个Windows窗体应用程序,我想使用BackgroundWorker。我创建了一个非常简单的例子,它非常有效:

public partial class Form1 : Form
{
    private BackgroundWorker bgw = new BackgroundWorker();
    public Form1()
    {
        InitializeComponent();

        bgw.WorkerReportsProgress = true;
        bgw.DoWork += new DoWorkEventHandler(DoWork);
        bgw.ProgressChanged += new ProgressChangedEventHandler(ProgressChanged);
        bgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Completed);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        bgw.RunWorkerAsync();
    }

    private void DoWork(object sender, DoWorkEventArgs e)
    {
        for (int i = 1; i <= 10; i++)
        {
            bgw.ReportProgress(i * 10, i.ToString());
            Thread.Sleep(1000);
        }
    }

    private void ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        label1.Text = string.Format("{0}% : Message = '{1}'", e.ProgressPercentage, e.UserState.ToString());
    }

    private void Completed(object sender, RunWorkerCompletedEventArgs e)
    {
        MessageBox.Show("Completed");
    }
}

现在,当我将相同的代码移动到我当前的应用程序时,它不会触发。唯一的区别是,我不是在表单级别运行代码,而是尝试在自定义用户控件中运行它。就这样:

public partial class LobbyForm : UserControl
{
    private BackgroundWorker bgw = new BackgroundWorker();

    public LobbyForm()
    {
        InitializeComponent();

        bgw.WorkerReportsProgress = true;
        bgw.DoWork += new DoWorkEventHandler(DoWork);
        bgw.ProgressChanged += new ProgressChangedEventHandler(ProgressChanged);
        bgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Completed);
    }

    public LobbyForm(List<TaskFile> tasks)
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        bgw.RunWorkerAsync();
    }

    private void DoWork(object sender, DoWorkEventArgs e)
    {
        for (int i = 1; i <= 10; i++)
        {
            bgw.ReportProgress(i * 10, i.ToString());
            Thread.Sleep(1000);
        }
    }

    private void ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        label5.Text = string.Format("{0}% : Message = '{1}'", e.ProgressPercentage, e.UserState.ToString());
    }

    private void Completed(object sender, RunWorkerCompletedEventArgs e)
    {
        MessageBox.Show("Completed");
    }
}

有关我是否遗失某些内容的任何想法?尝试从用户控件运行它可能是我误解的东西吗?

1 个答案:

答案 0 :(得分:0)

我只是复制了你的代码并对其进行了测试,如果你使用设计器拖放用户控件,它就能很好地工作。

但是,如果您在运行时创建控件并将其添加到表单中,请确保使用正确的构造函数。

LobbyForm lf = new LobbyForm();

这运行了这个构造函数:

public LobbyForm()
{
    InitializeComponent();

    bgw.WorkerReportsProgress = true;
    bgw.DoWork += new DoWorkEventHandler(DoWork);
    bgw.ProgressChanged += new ProgressChangedEventHandler(ProgressChanged);
    bgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Completed);
}

而不是

LobbyForm lf = new LobbyForm(tasks);

运行此构造函数(不挂接事件):

public LobbyForm(List<string> tasks)
{
    InitializeComponent();
}

解决方案(从第二个调用默认构造函数)

public LobbyForm(List<string> tasks) : this()
{
    //InitializeComponent();
}