5#include <QFutureWatcher>
6#include <QtConcurrentRun>
23 std::unique_ptr<QFutureWatcher<T>> future_watcher_;
25 std::atomic_bool stop_ =
false;
58 if (future_watcher_ && !future_watcher_->isFinished())
59 future_watcher_->waitForFinished();
80 future_watcher_ = std::make_unique<QFutureWatcher<T>>();
82 QObject::connect(future_watcher_.get(), &QFutureWatcher<T>::finished,
83 future_watcher_.get(), [
this]
87 future_watcher_.reset();
95 future_watcher_.reset();
99 future_watcher_->setFuture(QtConcurrent::run([
this]{
return parallel(stop_); }));
105 inline void stop() { stop_ =
true; }
108 inline bool isRunning()
const {
return future_watcher_.get(); }
114 future_watcher_->waitForFinished();
122 inline T
takeResult() {
return future_watcher_->future().takeResult(); }
Convenience class for recurring indexing tasks.
Definition backgroundexecutor.h:22
bool isRunning() const
Returns true if the asynchronous computation is currently running; otherwise returns false.
Definition backgroundexecutor.h:108
void waitForFinished()
Blocks until the current task finished.
Definition backgroundexecutor.h:111
~BackgroundExecutor()
Destructs the background executor.
Definition backgroundexecutor.h:54
std::function< T(const bool &abort)> parallel
The task to be executed in a thread.
Definition backgroundexecutor.h:34
void stop()
Stops the current execution.
Definition backgroundexecutor.h:105
std::function< void()> finish
The finish callback.
Definition backgroundexecutor.h:42
void run()
Run or schedule a rerun of the task.
Definition backgroundexecutor.h:68
T takeResult()
Takes the result from the future.
Definition backgroundexecutor.h:122
BackgroundExecutor()=default
Constructs the background executor.
The Albert namespace.
Definition app.h:55