How to measure thread time for progress bar?

0

I want to follow a thread progress. I have already implemented progress bar graphically, but I am wondering how to efficiently measure progress of the thread in a real time.

Progress bar

template<typename T>
inline T Saturate(T value, T min = static_cast<T>(0.0f), T max = static_cast<T>(1.0f))
{
    return value < static_cast<T>(min) ? static_cast<T>(min) : value > static_cast<T>(max) ? static_cast<T>(max) : value;
}

void ProgressBar(float progress, const Vector2& size)
{
    Panel* window = getPanel();

    Vector2 position = //some position                                                                                                                              
    progress = Saturate(progress);

    window->renderer->FillRect({ position, size }, 0xff00a5ff);
    window->renderer->FillRect(Rect(position.x, position.y, Lerp(0.0f, size.w, progress), size.h), 0xff0000ff);

    //progress will be shown as a %
    std::string progressText;       
    //ToString(value, how many decimal places)                                                                                                                        
    progressText = ToString(progress * 100.0f, 2) + "%";                                                

    const float textWidth = font->getWidth(progressText) * context.fontScale,
                textX = Clamp(Lerp(position.x, position.x + size.w, progress), position.x, position.x + size.w - textWidth);
    window->renderer->DrawString(progressText, Vector2(textX, position.y + font->getAscender(progressText) * context.fontScale * 0.5f), 0xffffffff, context.fontScale, *font.get());
}

and somewhere in a game loop, example usage

static float prog = 0.0f;
float progSpeed = 0.01f;
static float progDir = 1.0f;
prog += progSpeed * (1.0f / 60.0f) * progDir;

ProgressBar(prog, { 100.0f, 30.0f });

I know how to measure execution time:

uint t1 = getTime();
//... do sth
uint t2 = getTime();
uint executionTime = t2 - t1;

but of course progress bar will be updated after execution, so it won't be shown in a real time.

Should I use a new thread? Are there any other methods to do this?

c++
multithreading
progress-bar
asked on Stack Overflow Feb 27, 2019 by JarekBisnesu • edited Feb 27, 2019 by JarekBisnesu

1 Answer

0

All you can do for a progress bar is show an estimate or how long you have progressed based on the work you have already done (with an estimate (or maybe exact knowledge) of the total work to do.

All you know is what work has been done and the time that took. The time it takes to do everything is always going to be an estimate. You can usually do pretty good by basing the estimate on the already completed work, but not always.

Making an exact progress bar is (in most cases) Impossible. Best you can do is a guesstimate.

answered on Stack Overflow Feb 27, 2019 by Jesper Juhl • edited Feb 27, 2019 by Jesper Juhl

User contributions licensed under CC BY-SA 3.0