【发布时间】:2019-02-27 19:38:23
【问题描述】:
我想关注一个线程的进度。我已经以图形方式实现了进度条,但我想知道如何有效地实时测量线程的进度。
进度条
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());
}
在游戏循环中的某处,示例用法
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 });
我知道如何衡量执行时间:
uint t1 = getTime();
//... do sth
uint t2 = getTime();
uint executionTime = t2 - t1;
当然进度条会在执行后更新,所以不会实时显示。
我应该使用新线程吗?有没有其他方法可以做到这一点?
【问题讨论】:
-
可能取决于您使用的进度条类型。它是否有接口以估计时间属性的形式最大限度地传达?应该使用什么时间分辨率(实时不是真的,每一种实时都是基于一定的分辨率,操作系统能保证)?
-
它被编程为从 0.0f 到 1.0f。我认为时间可能以毫秒为单位,我会将其转换为 0.0f -> 1.0f
-
恐怕您必须更具体地了解您的 GUI 框架,您正在使用什么具体的
ProgressBar,您如何配置它,以及您打算如何更新它。我认为这应该是显示尝试的问题中包含的最少代码。 -
更改
do sth以定期报告进度.. -
好吧,我以为进度条是进度条,但如果是的话。我已经更新了问题。
标签: c++ multithreading progress-bar