Search code examples
c#backgroundworker

backgroundWorker: ReportProgress does not fire


I would like to add progress bar to my "app".

ProgressForm progressForm = new ProgressForm();
progressForm.paths.path1= pathSource1;
progressForm.paths.path2 = pathSource2;
progressForm.paths.path3= pathSource3;
progressForm.paths.path4=path4;
progressForm.paths.path5 = path5;
progressForm.ShowDialog();

During load event of progress form backgroundworker is fired.

private void ProgressForm_Load(object sender, EventArgs e)
{
    backgroundWorker1.RunWorkerAsync();
}

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{

    //some code
    backgroundWorker1.ReportProgress(1, "Loading Data From File");

    //some code
    backgroundWorker1.ReportProgress(0, "Loading Data From ...File");

    //some code
    backgroundWorker1.ReportProgress(10, "Loading Data From... File 2");

    //some code
    backgroundWorker1.ReportProgress(0, "hjhgjhgjfhgh");

    for (int i = 0; i < dataCollection.Count(); i++)
    {
        //some code
        backgroundWorker1.ReportProgress(((i+1) / data1.Count())*100, "");
        //some code
    }
}

WorkerReportsProgress is set to true, unfortunatelly ReportProgress method is not firing event ProgressChange (I set breakpoint there)

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    if (e.ProgressPercentage!=0)
    {
        progressBar.Value = e.ProgressPercentage;
    }
    if (e.UserState.ToString()!="")
    {
        lblProgressDesc.Text = e.UserState.ToString();
    }
}

What could be a cause of that ?


Solution

  • Ok, I am sorry, I found the reason. I do not why but VS did not treat mentioned method/event handler as an event of backgroundworker. I went to properties -> events -> clicked twice on ProgressChanged and it added new event handler: backgroundWorker1_ProgressChanged_1