add return value with async result

This commit is contained in:
王劲鹏
2021-05-25 10:45:45 +08:00
committed by osborn
parent 37f8898eac
commit da73ee5347
13 changed files with 95 additions and 94 deletions

View File

@@ -5,21 +5,43 @@
#include <QThreadPool>
#include <QtConcurrent/QtConcurrent>
#include "DoricAsyncResult.h"
class DoricAsyncCall {
public:
static void ensureRunInThreadPool(QThreadPool *threadPool,
std::function<void()> lambda) {
QFuture<std::function<void()>::result_type> future =
QtConcurrent::run(threadPool, lambda);
template <typename Function>
static std::shared_ptr<DoricAsyncResult>
ensureRunInThreadPool(QThreadPool *threadPool, Function &&function) {
std::shared_ptr<DoricAsyncResult> asyncResult =
std::make_shared<DoricAsyncResult>();
Function lambda = std::forward<Function>(function);
QtConcurrent::run(threadPool, [lambda, asyncResult]() {
lambda();
// asyncResult->setResult(result);
});
return asyncResult;
}
template <typename Function>
static void ensureRunInMain(Function &&function,
QThread *thread = qApp->thread()) {
static std::shared_ptr<DoricAsyncResult>
ensureRunInMain(Function &&function, QThread *thread = qApp->thread()) {
std::shared_ptr<DoricAsyncResult> asyncResult =
std::make_shared<DoricAsyncResult>();
auto *obj = QAbstractEventDispatcher::instance(thread);
Q_ASSERT(obj);
QMetaObject::invokeMethod(obj, std::forward<Function>(function));
Function lambda = std::forward<Function>(function);
QMetaObject::invokeMethod(obj, [lambda, asyncResult]() {
lambda();
// asyncResult->setResult(result);
});
return asyncResult;
}
};