title: Reporting progress of a LINQ query with background worker, how?
body: <p>I am exprementing an application which prints out all the possible multiplies of integers in an array that equals size of the array:</p>
  
  <pre><code>private void bgw_DoWork(object sender, DoWorkEventArgs e)
  {
      int progress = 0;
  
      int toCheck = int.Parse(textBox2.Text); //Number to check for
      int[] array = new int[toCheck];
  
      //Fill the array
      for (int i = 0; i &lt; toCheck; i++) 
      {
          array[i] = i;
          bgw.ReportProgress(progress);
          progress += 1;
      }
  
      var result =
          from i1 in array
          from i2 in array
          where i1 * i2 == toCheck
          let square = i1 * i2
          select new { i1, i2, square }; //how to report progress here?
  
      foreach (var res in result)
      {
          textBox1.Text += res.i1 + " * " + res.i2 + " = " + res.square + Environment.NewLine;
  
          bgw.ReportProgress(progress);
          progress += 1;
      } 
  }
  </code></pre>
  
  <p>The linq query itself is very time consuming specially if a big number should be checked for. Is there a way to report the progress of the linq query? Or I should leave linq and do this old-school mode?</p>
  
  <p><strong>Update</strong>
  This is my whole code. The progressbard does not fill after it got half-full. The first half is when the array is being created, the second part is when code tries to perform the linq query (That is why I think the reporting should be done in the linq query!) pardon my stupidity!!!</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Windows.Forms;
  using System.Diagnostics;
  
  namespace BGWorkerTest
  {
      public partial class Form1 : Form
      {
          private PerformanceCounter cpuCounter;
          private PerformanceCounter ramCounter;
          string[] diagnosticInfo = new string[2] { string.Empty, string.Empty };
  
          int toCheck = 0;
  
          StringBuilder sb;
  
          public Form1()
          {
              InitializeComponent();
  
              //Diagnostics
              cpuCounter = new PerformanceCounter();
  
              cpuCounter.CategoryName = "Processor";
              cpuCounter.CounterName = "% Processor Time";
              cpuCounter.InstanceName = "_Total";
  
              ramCounter = new PerformanceCounter("Memory", "Available KBytes");
          }
  
  
          private string[] GetDiagnostics()
          {
              diagnosticInfo[0] = string.Format("{0:0.##}", cpuCounter.NextValue()) + "%";
              diagnosticInfo[1] = ramCounter.NextValue() + "MB";
  
              return diagnosticInfo;
          }
  
          private void timerStStr_Tick(object sender, EventArgs e)
          {
              string[] temp = new string[2] { "", ""};
              temp = GetDiagnostics();
              ststrLabelCpu.Text = temp[0];
              ststrLabelMem.Text = temp[1];
          }
  
          private void button1_Click(object sender, EventArgs e)
          {
              ststrProgBar.Minimum = 0;
              ststrProgBar.Maximum = 0;
  
              toCheck = int.Parse(textBox2.Text);
  
              ststrProgBar.Minimum = 0;
              ststrProgBar.Maximum = toCheck * 2;
              ststrProgBar.Step = 1;
  
              //Starts the backgroundworker process asynchronously
              bgw.RunWorkerAsync();           
          }
  
          private void bgw_DoWork(object sender, DoWorkEventArgs e)
          {
              int progress = 0;
  
              int[] array = new int[toCheck + 1];
  
              for (int i = 0; i &lt; toCheck + 1; i++)
              {
                  array[i] = i;
                  bgw.ReportProgress(progress);
                  progress += 1;
              }
  
              var result =
                  from i1 in array
                  from i2 in array
                  where i1 * i2 == toCheck
                  let square = i1 * i2
                  select new { i1, i2, square };
  
              sb = new StringBuilder();
  
              foreach (var res in result)
              {
                 sb.AppendLine(res.i1 + " * " + res.i2 + " = " + res.square);
  
                 bgw.ReportProgress(progress);
                 progress += 1;
  
                 Application.DoEvents();
              } 
          }
  
          private void bgw_ProgressChanged(object sender, ProgressChangedEventArgs e)
          {
              ststrProgBar.PerformStep();
          }
  
          private void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
          {
              if (e.Cancelled)
              {
                  MessageBox.Show("Operation Cancelled");
              }
              else
              {
                  MessageBox.Show("Operation Completed");
  
                  textBox1.Text = sb.ToString();
              }
          }
      }
  }
  </code></pre>