5#include <QFutureWatcher>
31 std::function<void(T && results)>
finish;
37 QFutureWatcher<T> future_watcher_;
49 finish(std::move(future_watcher_.future().takeResult()));
50 }
catch (
const std::exception &e) {
51 WARN <<
"Exception in BackgroundExecutor::finish"
52 << QString::fromStdString(e.what());
54 WARN <<
"Unknown exception in BackgroundExecutor::finish.";
59 T run_(
const bool &abort)
62 auto start = std::chrono::system_clock::now();
64 auto end = std::chrono::system_clock::now();
65 runtime = std::chrono::duration_cast<std::chrono::milliseconds>(end-start);
67 }
catch (
const std::exception &e) {
68 WARN <<
"Exception in BackgroundExecutor::parallel" << QString::fromStdString(e.what());
70 WARN <<
"Unknown exception in BackgroundExecutor::parallel.";
77 QObject::connect(&future_watcher_, &QFutureWatcher<T>::finished, [
this](){onFinish();});
83 WARN <<
"Busy wait for BackgroundExecutor task. Abortion handled correctly?";
84 auto start = std::chrono::system_clock::now();
85 future_watcher_.waitForFinished();
86 auto end = std::chrono::system_clock::now();
87 auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end-start);
88 WARN << QStringLiteral(
"Busy waited for %1 ms.").arg(duration.count());
96 if (future_watcher_.isRunning())
104 bool isRunning()
const {
return future_watcher_.isRunning(); }
Provides a lean interface for recurring indexing tasks.
Definition backgroundexecutor.h:19
bool isRunning() const
Returns true if the asynchronous computation is currently running; otherwise returns false.
Definition backgroundexecutor.h:104
~BackgroundExecutor()
Definition backgroundexecutor.h:80
std::chrono::milliseconds runtime
The runtime of the last execution of parallel.
Definition backgroundexecutor.h:34
std::function< void(T &&results)> finish
The results handler.
Definition backgroundexecutor.h:31
std::function< T(const bool &abort)> parallel
The task that should be executed in background.
Definition backgroundexecutor.h:25
void run()
Run or schedule a rerun of the task.
Definition backgroundexecutor.h:95
BackgroundExecutor()
Definition backgroundexecutor.h:76
#define WARN
Creates a log object (level warning) you can use to pipe text into (<<).
Definition logging.h:20