Progress and Time Bar

Asked

Viewed 1,746 times

4

I would like to know what is the best function in C# that returns with the remaining time when the progress bar reaches the end.

I tried this:

int restante = Math.Min(progressBar1.Maximum, progressBar1.Value);

and this too

int restante = Math.Max(progressBar1.Maximum, progressBar1.Value);

but what I got was just the time it takes to complete progress

This code had worked but now it doesn’t work:

var i=progressBar1.Maximum / progressBar1.Value * 1000;

but he says the split was a zero result.

1 answer

5


A progress bar is just a representation visual of any scale to indicate the progress of any activity. It uses a scale to represent this progress, but the information presented can be of time (when x has passed), content (how much has been processed x how much remains to process), or anything else you can imagine (even for setting a variable in a predefined range - although more modern tools provide their own objects for this).

In any case, the choice of what will be represented is up to the system developer. Only the "implementation" is always the same: usually the data structures provided by languages such as C# for the construction of progress bars have three properties that are used to facilitate the "drawing" of the bar. Are they:

  • Max: Stores the maximum value of the chosen scale.
  • Min: Stores the minimum value of the chosen scale.
  • Value: Stores the current value in the chosen scale. Starts in Min and goes to Max.

So consider the following example:

Suppose you wish to download a 500 byte file (let’s call this size n) and use a progress bar to indicate the progress of this download. Instead of representing the "time" you will represent the "size" simply by making the bar move as bytes are downloaded.

At first nothing will have been downloaded, so it makes sense to imagine that the start (the Min property) will be 0. At the end the file will have been completely downloaded, which can mean (depending on an OWN choice) that (A) the value of the Max property will be n (ie 500 - total bytes) or (B) 100 (equivalent to say that 100% of the file has been downloaded).

If you choose to represent the progress in direct terms of bytes (A), as the file is downloaded (in general you capture an event to be notified of the progress of the download) you will count the number of bytes already downloaded (let’s call this accumulated value of i) and simply update the Value property with the value of i: Value = i.

If you choose to represent the progress in terms of the percentage of the downloaded file (B), as the file is downloaded you will need to account for this percentage, which is easily calculated by dividing the downloaded total (i) by the original file size (n)then update the Value property with it: Value = i / n.

If you want to use a progress bar to provide an estimate of time of some task, you need to calculate by yourself two things:

  1. The time elapsed from the start of the task to the moment.
  2. The total "size" of the task and what has already been accomplished (such as the number of bytes downloaded from the previous example).

So, you can - at predefined intervals - calculate a simple rule of three to get to the following result: if you took time (t) to process the size (i) already downloaded, you will probably take time (t') to process the total size (n), if the conditions for carrying out the task remain the same.

That calculation is simple. Divide the elapsed time (t) by the "size" already processed (i), and multiply this "factor" by the total size (n), using this result as the value for Max and the elapsed time as the value for Value: Max = n * t / i and Value = t.

Remarks:

  • It must have been easy to realize that there is no miracle: you need to know the final size of your "task" to be able to show a bar of progress that is consistent with reality. But that’s not always feasible. Imagine a program that needs to delete all files with a given extension in all folders and subfolders of a very large disk. To calculate the "size" for any of the previous approaches the program would first need to go through all the files to countlos, but this is the same as doing the originally intended task and that is why in many cases one does not use a progress bar but rather a mere indicator that the program did not lock (something just rotating on the screen without any scale).

  • Even having the final size of your task, time estimation is often uncertain. For example, downloading files depends on the bandwidth of the network that may vary according to the existing traffic. So it’s an estimate. You don’t usually use progress bars to indicate this visually, because it may seem strange to the user to see a progress bar "go backwards" (in case the estimate worsens by network latency, for example). That’s why tools like Torrents downloa programs present the two pieces of information: a progress bar to consistently indicate the download progress in terms of file size (in bytes) and textual information of the seconds esteemed for termination (calculated in a manner similar to that indicated).


P.S.: Based on all that has been explained, your last line of code certainly generates error of division by zero because the value of the Value property is equal to 0, which is natural at the beginning of a task. Provavelment you wanted to do the opposite, and divide the current value (Value) by the maximum (Max) to get a percentage and then apply it to the value 1000.

P.P.S.: "Time remaining" and "Time to complete progress" seem to me the same thing. You are confused in your question.

  • I want a function that returns with the remaining time to complete the progress.

  • 1

    @Nathan130200 As I said in the answer, the remaining time needs to be estimated from your problem. I’m pretty sure C# doesn’t have a function ready for that.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.