This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
Doric/doric-Qt/doric/async/DoricAsyncCall.h

27 lines
721 B
C
Raw Normal View History

2021-02-04 16:59:58 +08:00
#ifndef ASYNC_CALL_H
#define ASYNC_CALL_H
2021-02-08 11:37:51 +08:00
#include <QFuture>
2021-02-04 16:59:58 +08:00
#include <QThreadPool>
#include <QtConcurrent/QtConcurrent>
class DoricAsyncCall {
public:
static void ensureRunInThreadPool(QThreadPool *threadPool,
std::function<void()> lambda) {
2021-02-08 11:37:51 +08:00
QFuture<std::function<void()>::result_type> future =
QtConcurrent::run(threadPool, lambda);
2021-02-04 16:59:58 +08:00
}
2021-02-09 10:38:27 +08:00
template <typename Function>
2021-02-22 18:54:29 +08:00
static void ensureRunInMain(Function &&function,
QThread *thread = qApp->thread()) {
auto *obj = QAbstractEventDispatcher::instance(thread);
Q_ASSERT(obj);
QMetaObject::invokeMethod(obj, std::forward<Function>(function));
2021-02-09 10:38:27 +08:00
}
2021-02-04 16:59:58 +08:00
};
#endif // ASYNC_CALL_H