Utilizing the CProgressCtrl :: MFC

kuphryn

Senior member
Jan 7, 2001
400
0
0
Hi.

Class CProgressCtrl is functionally simple and easy to understand. Nonetheless, I do have one question. How do you calculate the time duration from the start of a job (0% progression) to the completion (100% progression).

For example, let say we have a program that converts all letters in a text file into uppercase.

Original text file:

a
b
c

Output text file:

A
B
C

How do you implement a progression bar such that it displays the completetion animation accurately? I know the example above takes less than a second, but lets just imagine that it takes 10 *or* more.

Thanks,
Kuphryn
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
filesize = myfile.GetLength ( )
do {
- chunksize = read a chunk // say 4096 bytes, will get less on last read
- if (chunksize > 0) process the chunk
- partdone += chunksize
- progress = filesize / partdone
- update progress control using "progress"
} while (chunksize > 0)
 

RSMemphis

Golden Member
Oct 6, 2001
1,521
0
0
Well, let's say you would have to do a complex operation (hacking every single password entry of a database by brute force).

Then, reading and writing takes next to no time. You read everything in and find out how many strings there are.
so,
n = number of strings;
Then you have a variable x which specifies how many strings you converted.

percent = (100*x)/n;
myProgressBar.SetPos(percent);

that's how I would do it. Roughly.
 

kuphryn

Senior member
Jan 7, 2001
400
0
0
Thanks.

To my understanding, there no accurate way to handle the process completion percentage. We can approximate. Approximating is good enough for some programs.

Kuphryn
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126


<< To my understanding, there no accurate way to handle the process completion percentage. We can approximate. Approximating is good enough for some programs. >>



Yes, it's good enough 99% of the time. Users mostly want to know that the program hasn't crashed / frozen on them during a long process and care much less whether the progress bar is showing 48% instead of the "proper" 51%.

It's ideal if the bar reaches 50% when roughly half of the total time has elapsed, but it's not always possible and you'll find many existing programs that don't do this.