title: How to calculate the time remaining to complete an operation?
body: <p>This is what I have so far, but it doesn't matter to what number I change the progressbar1.value is changed too, I still get the same result 0. I am guessing that the problem lies in the timespent Variable, but I can't figure it out.   </p>
  
  <pre><code>using System;
  using System.Threading;
  using System.Collections.Generic;
  using System.Linq;
  
  
  namespace Rextester
  {
      public class Program
      {
      public static void Main(string[] args)
      {            
          private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
      {
          int sum = 0;
          for (int i = 1; i &lt;= 100; i++)
          {
              // run the process here
             Thread.Sleep(100);
              // end process
              sum = sum + i;
              backgroundWorker1.ReportProgress(i);
  
  
  
              if (backgroundWorker1.CancellationPending)
              {
                  e.Cancel = true;
                  backgroundWorker1.ReportProgress(0);
                  return;
              }
          }
          // displays the sum of the process
          e.Result = sum;
  
      }
  
      private void backgroundWorker1_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
      {
          progressBar1.Value = e.ProgressPercentage;
          label5.Text = e.ProgressPercentage.ToString() + "%";
  
          DateTime starttime = DateTime.Now;
          var timespent = DateTime.Now - starttime;
          double secondsremaining = (double)(timespent.TotalSeconds / progressBar1.Value) * (progressBar1.Maximum - progressBar1.Value);
          label7.Text = "Time remaining:" + (int)secondsremaining;
          Console.WriteLine(secondsremaining);
  
      }
  
      private void backgroundWorker1_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
      {
          if (e.Cancelled)
          {
              label5.Text = "Processing Cancelled";
          }
          else if (e.Error != null)
          {
              label5.Text = e.Error.Message;
          }
          else
          {
              label5.Text = "Sum = " + e.Result.ToString();
          }
          label6.Text = "";
      }
  
      private void button11_Click_1(object sender, EventArgs e)
      {
          //Checks if the background worker is busy running operations
          if (!backgroundWorker1.IsBusy)
          {
              //this method will start the excution of the opreation
              backgroundWorker1.RunWorkerAsync();
  
          }
          else
          {
             label6.Text = "The operation is being completed, please wait...";
          }
  
      }
     }
    }
  }
  </code></pre>