Filter out redundant progress updates sooner

Sending events from background threads to the main thread is somewhat
expensive, so filter out progress updates which don't actually change
the progress on the background thread.
This commit is contained in:
Thomas Goyne 2014-05-03 16:46:17 -07:00
parent 6a1321e1df
commit d672e75109
1 changed files with 8 additions and 7 deletions

View File

@ -72,14 +72,11 @@ namespace {
class DialogProgressSink final : public agi::ProgressSink { class DialogProgressSink final : public agi::ProgressSink {
DialogProgress *dialog; DialogProgress *dialog;
std::atomic<bool> cancelled; std::atomic<bool> cancelled{false};
int progress = 0;
public: public:
DialogProgressSink(DialogProgress *dialog) DialogProgressSink(DialogProgress *dialog) : dialog(dialog) { }
: dialog(dialog)
, cancelled(false)
{
}
void SetTitle(std::string const& title) override { void SetTitle(std::string const& title) override {
Main().Async([=]{ dialog->title->SetLabelText(to_wx(title)); }); Main().Async([=]{ dialog->title->SetLabelText(to_wx(title)); });
@ -90,7 +87,11 @@ public:
} }
void SetProgress(int64_t cur, int64_t max) override { void SetProgress(int64_t cur, int64_t max) override {
Main().Async([=]{ dialog->SetProgress(mid<int>(0, double(cur) / max * 300, 300)); }); int new_progress = mid<int>(0, double(cur) / max * 300, 300);
if (new_progress != progress) {
progress = new_progress;
Main().Async([=]{ dialog->SetProgress(new_progress); });
}
} }
void Log(std::string const& str) override { void Log(std::string const& str) override {