5#include <QFutureWatcher>
6#include <QtConcurrentRun>
23 std::unique_ptr<QFutureWatcher<T>> future_watcher_;
25 std::atomic_bool stop_ =
false;
63#
if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
64 && !future_watcher_->isFinished())
66 && future_watcher_->isRunning())
68 future_watcher_->waitForFinished();
89 future_watcher_ = std::make_unique<QFutureWatcher<T>>();
91 QObject::connect(future_watcher_.get(), &QFutureWatcher<T>::finished,
92 future_watcher_.get(), [
this]
96 future_watcher_.reset();
104 future_watcher_.reset();
108 future_watcher_->setFuture(QtConcurrent::run([
this]{
return parallel(stop_); }));
114 inline void stop() { stop_ =
true; }
117 inline bool isRunning()
const {
return future_watcher_.get(); }
123 future_watcher_->waitForFinished();
131 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:117
void waitForFinished()
Blocks until the current task finished.
Definition backgroundexecutor.h:120
~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:114
std::function< void()> finish
The finish callback.
Definition backgroundexecutor.h:42
void run()
Run or schedule a rerun of the task.
Definition backgroundexecutor.h:77
T takeResult()
Takes the result from the future.
Definition backgroundexecutor.h:131
BackgroundExecutor()=default
Constructs the background executor.
The Albert namespace.
Definition app.h:55