title: C# Winform ProgressBar and BackgroundWorker
body: <p>I have the following problem:</p>
  
  <p>I have a Form named MainForm. I have a long operation to be taken place on this form. </p>
  
  <p>While this long operation is going on, I need to show another from named ProgressForm on top of the MainForm. </p>
  
  <p>ProgressForm contains a progress bar which needs to be updated while the long operation is taking place.</p>
  
  <p>After the long operation is completed, the ProgressForm should be closed automatically.</p>
  
  <p>I have written the following code:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Text;
  using System.Windows.Forms;
  using System.Threading;
  
  namespace ClassLibrary
  {
      public class MyClass
      {
          public static string LongOperation()
          {
              Thread.Sleep(new TimeSpan(0,0,30));
  
              return "HelloWorld";
          }
      }
  }
  
  using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Text;
  using System.Windows.Forms;
  
  namespace BackgroungWorker__HelloWorld
  {
      public partial class ProgressForm : Form
      {
          public ProgressForm()
          {
              InitializeComponent();
          }
  
          public ProgressBar ProgressBar
          {
              get { return this.progressBar1; }
              set { this.progressBar1 = value; }
          }
      }
  }
  
  using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Text;
  using System.Windows.Forms;
  
  using ClassLibrary;
  
  namespace BackgroungWorker__HelloWorld
  {
      public partial class MainForm : Form
      {
          ProgressForm f = new ProgressForm();
  
          public MainForm()
          {
              InitializeComponent();  
          }
  
          int count = 0;
          private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
          {
              if (f != null)
              {
                  f.ProgressBar.Value = e.ProgressPercentage;
              }
  
              ++count;
          }
  
          private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
          {
              if (e.Cancelled) 
              {  
                  MessageBox.Show("The task has been cancelled");  
              }  
              else if (e.Error != null)  
              {                  
                  MessageBox.Show("Error. Details: " + (e.Error as Exception).ToString());  
              }  
              else 
              {  
                  MessageBox.Show("The task has been completed. Results: " + e.Result.ToString());  
              }
          }
  
  
          private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
          {
              if (f == null)
              {
                  f = new ProgressForm();
              }
  
              f.ShowDialog();
  
              //backgroundWorker1.ReportProgress(100);
  
              MyClass.LongOperation();
  
              f.Close();
          }
  
          private void btnStart_Click(object sender, EventArgs e)
          {
              backgroundWorker1.RunWorkerAsync();
          }
  
          private void btnCancel_Click(object sender, EventArgs e)
          {
              backgroundWorker1.CancelAsync();
  
              this.Close();
          }
      }
  }
  </code></pre>
  
  <p>I can't find a way to update the <code>progressBar</code>. </p>
  
  <p>Where should I place <code>backgroundWorker1.ReportProgress()</code> and how should I call this?</p>
  
  <p>I must not make any change in <code>MyClass</code> because I don't know what would happen or how long it takes to complete the operation in this layer of my application.</p>
  
  <p>Can anyone help me?</p>