0
votes

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 ?

4

4 Answers

0
votes

The following code example demonstrates the use of the ProgressChanged event to report the progress of an asynchronous operation to the user. This code example is part of a larger example provided for the BackgroundWorker class.

// This event handler updates the progress bar. 
private void backgroundWorker1_ProgressChanged(object sender,
    ProgressChangedEventArgs e)
{
    this.progressBar1.Value = e.ProgressPercentage;
}
0
votes

I had a similar issue, where the ProgressChanged and RunWorkerCompleted events weren't firing. I was starting the RunWorkerAsync process from the UI thread and then sitting in a sleep loop waiting for the IsBusy flag to change. Turned out I needed to add an Application.DoEvents to the loop in order for the events to fire.

-1
votes

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