Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating the progress percentage

How I calculate the percentage of a file that is loading in loop?

For example:

ProcessStartInfo p = new ProcessStartInfo();
Process process = Process.Start(p);
StreamReader sr = process.StandardOutput;
char[] buf = new char[256];
string line = string.Empty;
int count;

while ((count = sr.Read(buf, 0, 256)) > 0)
{
    line += new String(buf, 0, count);
    progressBar.Value = ???
}

`

How I do this? Thanks in advance

like image 626
Kakashi Avatar asked Sep 19 '11 06:09

Kakashi


People also ask

How do you calculate percentage progress?

Lastly, to determine the percentage of work that should have been completed by today (Planned Progress %) the number of completed days is divided by the total number of days and shown as a percentage. Therefore, (15.5 days / 50.75 days) x 100 percent = 30.54 percent,which rounds up to 31 percent.

How do I calculate percentage progress in Excel?

Calculate Percentage in Excel (Basic Method)The formula =C2/B2 should be entered in cell D2 and copied to any number of rows you require. You can view the resulting decimal fractions as percentages by clicking the Percent Style button (Home tab > Number group).

How do you calculate percentage in progress bar?

Each page's progress percentage-value is determined by the number of total pages in the survey. For a five-page survey, we take 100% and divide by 5, resulting in 20% progress per page. The 20% per page value is applied as follows: 20% progress after Page One is submitted.


1 Answers

You'd need to know the eventual amount of output to expect - otherwise you have no way of giving a proportion of the output which has already been completed.

If you know it's going to be a certain size, you can use:

// *Don't* use string concatenation in a loop
StringBuilder builder = new StringBuilder();
int count;
while ((count = sr.Read(buf, 0, 256)) > 0)
{
    builder.Append(buf, 0, count);
    progressBar.Value = (100 * builder.Length) / totalSize;
}

This assumes a progress bar with a minimum of zero and a maximum of 100 - it also assumes that the overall length is less than int.MaxValue / 100. Another approach is simply to make the progress bar maximum value the overall length, and set the progress bar value to builder.Length.

You'll still need to know the overall length before you start though, otherwise you can't possibly give progress as a proportion.

like image 118
Jon Skeet Avatar answered Oct 25 '22 12:10

Jon Skeet