title: Adding progress bar monitoring for StreamReader?
body: <p>I am writing a fairly simple windows form C# application in Visual Studio 2015. Here is one of my handlers:</p>
  
  <pre><code>private void button2_Click(object sender, EventArgs e)
  {
      listBoxCodes.Items.Clear();
      listBoxDuplicates.Items.Clear();
  
      Cursor.Current = Cursors.WaitCursor;
      Application.DoEvents();
  
      progressBar.Value = 0;
      using (var reader = new StreamReader(textBoxGENIO.Text))
      {
          // progressBar is set for 5 unit intervals (from 0 to 100)
  
          // How can I show % complete reading the file?
  
          string line;
          while ((line = reader.ReadLine()) != null)
          {
              if(line.Length &gt; 8 &amp;&amp; line.Substring(0, 4) == "080,")
              {
                  string strCode = line.Substring(4, 4);
  
                  if (listBoxCodes.FindStringExact(strCode) == -1)
                      listBoxCodes.Items.Add(strCode);
                  else
                      listBoxDuplicates.Items.Add(strCode);
              }
          }
      }
  
      Cursor.Current = Cursors.Default;
  }
  </code></pre>
  
  <p>Now, I understand that I can read the file through once, to get the total number of lines, and then read the file through again, and do progress monitoring based on the percentage through the line count.</p>
  
  <p>But I want to avoid reading the file twice because they can get very large.</p>
  
  <p>Is there any way that I can use the file position / file size instead to establish the percentage complete?</p>
  
  <p>I can't see any <strong>Position</strong> method in the <strong>StreamReader</strong> object.</p>
  
  <p>Thank you.</p>
  
  <p>Update: Currently looking at this question which may be the answer:</p>
  
  <p><a href="https://stackoverflow.com/questions/10189270/tracking-the-position-of-the-line-of-a-streamreader">Tracking the position of the line of a streamreader</a></p>
  
  <p>Update:</p>
  
  <p>My calculations seem to be wrong:</p>
  
  <pre><code>private void button2_Click(object sender, EventArgs e)
  {
      listBoxCodes.Items.Clear();
      listBoxDuplicates.Items.Clear();
  
      Cursor.Current = Cursors.WaitCursor;
      Application.DoEvents();
  
      int iLastPercentage = -1;
  
      progressBar.Value = 0;
      using (var reader = new StreamReader(textBoxGENIO.Text))
      {
          string line;
          while ((line = reader.ReadLine()) != null)
          {
              // progressBar is set for 5 unit intervals (from 0 to 100)
  
              // How can I show % complete reading the file?
              int iPercentage = Convert.ToInt32(((double)reader.BaseStream.Length / 100.0) * (double)reader.BaseStream.Position);
              if (iLastPercentage == -1 || (iPercentage - iLastPercentage &gt;= 5))
              {
                  progressBar.PerformStep();
                  iLastPercentage = iPercentage;
                  Application.DoEvents();
              }
  
              if (line.Length &gt; 8 &amp;&amp; line.Substring(0, 4) == "080,")
              {
                  string strCode = line.Substring(4, 4);
  
                  if (listBoxCodes.FindStringExact(strCode) == -1)
                      listBoxCodes.Items.Add(strCode);
                  else
                      listBoxDuplicates.Items.Add(strCode);
              }
          }
      }
  
      Cursor.Current = Cursors.Default;
  }
  </code></pre>
  
  <p>}</p>