user3733946
user3733946

Reputation: 21

C# Progress Bar based on number of lines in file

I would like to perform the following action in C#:

  1. Read the amount of lines in a specific text file.
  2. Depending on the amount of lines in a text file, read each line and update the progress bar.

This is what I have so far:

private void Form1_Load(object sender, System.EventArgs e)
{
    backgroundWorker1.RunWorkerAsync();
}

private void bw_DoWork(object sender, DoWorkEventArgs e)
{
    int lineCount = 0;
    int max = 100;
    float percent;
    using (var reader = File.OpenText(@"C:\file.txt"))
    {
        toolStripLabel1.Text = "Initializing...";

        while (reader.ReadLine() != null)
        {
            lineCount++;
        }
        reader.Close();
        for (int i = 0; i < lineCount; i++)
        {
            percent = (max / lineCount);
            toolStripLabel1.Text = i.ToString() + " - " + percent + "%";
            bw.ReportProgress(Convert.ToInt32(percent));
            percent = percent + percent;
            // Thread.Sleep(100);

        }
    }
}

private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    progressBar1.Value = e.ProgressPercentage;
    // progressBar1.Value = (int)(((decimal)currentPosition / (decimal)length) * (decimal)100);
    this.Text = e.ProgressPercentage.ToString();
}

Anyone have an idea on how to properly calculate and display the progress bar depending on what line is being read from the file?

Thanks in advance!

Upvotes: 2

Views: 4837

Answers (3)

MonauralMass371
MonauralMass371

Reputation: 11

Try this:

private void InvokeLabel(string text)
{
    if (toolStripLabel1.InvokeRequired)
    {
        toolStripLabel1.Invoke(new Action<string>(InvokeLabel), text);
    }
    else
    {
        toolStripLabel1.Text = text;
    }
}

For your progress bar it's the same code

Upvotes: 0

Chris
Chris

Reputation: 5514

There are several problems with your code:

  1. You're first reading the file, then after it has all been read you start updating the progress bar, which doesn't really make any sense.
  2. You're doing integer division in your percentage calculation.
  3. The percentage calculation isn't quite right.
  4. You're updating the ToolStripLabel from the worker thread.

Try this:

private void bw_DoWork(object sender, DoWorkEventArgs e)
{
    var files = File.ReadAllLines( @"C:\file.txt" );
    for( int i = 0; i < files.Length; i++ )
    {
        var line = files[i];

        // do work on the current line here

        int percentage = (int)( ( i / (double)files.Length ) * 100.0 );
        bw.ReportProgress( percentage );
    }
}

private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    progressBar1.Value = e.ProgressPercentage;
    toolStripLabel1.Text = e.ProgressPercentage.ToString() + "%";
    this.Text = e.ProgressPercentage.ToString();
}

If the file is very large and it's the actual reading that takes time, you should go back to your original way of reading it, but update the progress for every line instead of after. Though then you're back to the issue of not knowing how many lines there are before you have read the file. In that case - you could estimate the progress based on how many bytes/chars you have read compared to the full size of the file, which you can get without reading it all.

Upvotes: 4

Shachaf.Gortler
Shachaf.Gortler

Reputation: 5735

There are a few things you need to do try this : replace

percent = (max / lineCount);

with this

percent = (100.0 * i / lineCount);

and remove percent = percent + percent;

Upvotes: 0

Related Questions