tags:

views:

181

answers:

2

Hi, Here is the problem: I want to show a progress bar (just as a text like "Remaining 35%...") during a C++ function execution. I've done the first part which is the progress bar but the problem is how do I show the progress bar during the other functions execution?

I just want to start showing the bar when execution enters a specific function and reach 100% when leaving the function. How do I do this in C++? any suggestion? thanks in advanced!

/Niklas

+1  A: 

Use a separate thread to update the progress bar. That should give "near real" progress of your application.

Amit
Good advice in general, bad advice for a Windows program - only one thread should interact with the GUI, or bad things happen. Do the opposite, run the function in a worker thread and leave the main one to work the status bar.
Mark Ransom
No experience in the Win32 space. Thanks for pointing out.
Amit
"only one thread should interact with the GUI, or bad things happen" -> good advice for any program with a GUI on any platform.
foxwoods
A: 

There are two things to consider. First is, when do you update your progress bar? In general this can be done by dividing the work your function does into ticks, and then use a tick-count that you increase every time the function has advanced one tick. One example would be this:

void SomeFunction() {
    for ( int i = 0; i < 1337; i++ ) {
        // do important stuff
        ProgressBar.IncreaseTickCount();
    }
}

The caller would then do something like this:

ProgressBar.SetNumberOfTicks( 1337 );
SomeFunction();

This way, the progress bar would be able to recalculate its value at every iteration of the loop. How this is presented on the screen depends on your GUI-Framework. As has been suggested by @Amit and the comments to his answer, you will most likely have the function run in a worker-thread, and the UI-thread will update the progress bar continuously. In this case make sure that the tick-counter is thread-safe.

Space_C0wb0y