-1

I have a 2 forms, MainForm and ProgressForm.

#1 MainForm.cs has a button, which when clicked starts my backgroundWorker1.

 ProgressForm pg = new ProgressForm(); 

 private void StarBtn_Click(object sender, EventArgs e)
        {
           
           pg.Show();
           
            if(!backgroundWorker1.IsBusy)
            {
                backgroundWorker1.RunWorkerAsync();
            }
        }

 private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
          //time consuming tasks...
        }

#2 ProgressForm is where I have added a progress bar and a label.

namespace MainApp
{
    public partial class ProgressForm : Form
    {
        public ProgressForm()
        {
            InitializeComponent();
        }

    }
}

I need to show the progress of my background process in MainForm to progressBar1 in ProgressForm (as below)

 private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
           //something like..
           //this.progressBar1.Value = e.ProgressPercentage;
           //in ProgressForm...
            
        }

and show a completed in label and close the form.

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
           //label.Text = "Completed"
          // Close the ProgressForm...
            
        }

How can I access the elements in ProgressFrom from backgroundworker in MainForm and make the progress update.

Sid133
  • 354
  • 2
  • 17
  • 1
    As a side note the `BackgroundWorker` class is [technologically obsolete](https://stackoverflow.com/questions/12414601/async-await-vs-backgroundworker/64620920#64620920). A better way of offloading work to a background thread is the `Task.Run` method, combined with async/await. – Theodor Zoulias Jul 01 '21 at 09:27

1 Answers1

1

Your background worker events are in the MainForm? If so then add public properties to your ProgressForm and set the label and / or progress bar accordingly to the provided values.

ProgressForm

public int Progress {
set {
    progressBar1.Value = value;
  }
}
public string LabelText{
set {
   label.Text = value;
  }
}

MainForm

pg.Progress = e.ProgressPercentage;
mikehachen
  • 256
  • 1
  • 2
  • 8
  • Tried this but progress bar is not updating. Even tried this.Invoke((MethodInvoker)delegate { inside set command. Still not updating – Sid133 Jul 01 '21 at 08:09
  • I use the same approach in different projects. So far it has always worked. Where do you set the two properties? In which event? – mikehachen Jul 01 '21 at 11:20
  • I used the 'set' property on ProgressForm and in MainForm, I called pg.Progress = e.ProgressPercentage; inside backgroundWorker1_ProgressChanged event. Any initilization of progressbar is required? – Sid133 Jul 01 '21 at 12:57
  • Is your ProgressChanged event called? Did you set backgroundWorker1.WorkerReportsProgress = true; – mikehachen Jul 01 '21 at 14:18
  • backgroundWorker1.WorkerReportsProgress = true is set already. But do I need to call ProgressChanged ?I thought it responded like backgroundWorker1_RunWorkerCompleted which give response once RunWorkerAsync is completed. – Sid133 Jul 01 '21 at 15:09
  • 1
    You have to call ProgressChanged yourself. The BackgroundWorker doesn't know how much of your job is already done. – mikehachen Jul 01 '21 at 15:37
  • I don't have a 'for loop' to report progress of each step. I have 2 functions api.initilize(); and api.start() ,each taking 3-4 secs to finish execution. How can I report the progress then? hardcoding backgroundWorker1.ReportProgress(50) on completion of api.initilize(); and again backgroundWorker1.ReportProgress(50) on completion of api.start() – Sid133 Jul 01 '21 at 15:48