mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-04 16:56:53 +08:00
Issue #77 Move singleShot from worker.h to threadutils.h
This commit is contained in:
@@ -42,29 +42,6 @@
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
/*!
|
||||
* Starts a single-shot timer which will call a task in the thread of the given object when it times out.
|
||||
*
|
||||
* Differs from QTimer::singleShot in that this implementation interacts better with QObject::moveToThread,
|
||||
* and returns a QFuture which can be used to detect when the task has finished or obtain its return value.
|
||||
*/
|
||||
template <typename F>
|
||||
auto singleShot(int msec, QObject *target, F &&task)
|
||||
{
|
||||
CPromise<decltype(task())> promise;
|
||||
QSharedPointer<QTimer> timer(new QTimer, [](QObject * o) { QMetaObject::invokeMethod(o, &QObject::deleteLater); });
|
||||
timer->setSingleShot(true);
|
||||
timer->moveToThread(target->thread());
|
||||
QObject::connect(timer.data(), &QTimer::timeout, target, [trace = getStackTrace(), task = std::forward<F>(task), timer, promise]() mutable
|
||||
{
|
||||
static_cast<void>(trace);
|
||||
timer.clear();
|
||||
promise.setResultFrom(task);
|
||||
});
|
||||
QMetaObject::invokeMethod(timer.data(), [t = timer.data(), msec] { t->start(msec); });
|
||||
return promise.future();
|
||||
}
|
||||
|
||||
/*!
|
||||
* Just a subclass of QThread whose destructor waits for the thread to finish.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user