mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-17 10:55:32 +08:00
Ref T105, m_updateTimer as protected member of CContinuousWorker
* stop timer * default cleanup implementation * set timer name
This commit is contained in:
committed by
Mathew Sutcliffe
parent
52755e59c8
commit
4a7ad09e46
@@ -116,6 +116,12 @@ namespace BlackMisc
|
|||||||
return thread()->isInterruptionRequested();
|
return thread()->isInterruptionRequested();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CContinuousWorker::CContinuousWorker(QObject *owner, const QString &name) :
|
||||||
|
m_owner(owner), m_name(name)
|
||||||
|
{
|
||||||
|
m_updateTimer.setObjectName(name + ":timer");
|
||||||
|
}
|
||||||
|
|
||||||
void CContinuousWorker::start(QThread::Priority priority)
|
void CContinuousWorker::start(QThread::Priority priority)
|
||||||
{
|
{
|
||||||
BLACK_VERIFY_X(!hasStarted(), Q_FUNC_INFO, "Tried to start a worker that was already started");
|
BLACK_VERIFY_X(!hasStarted(), Q_FUNC_INFO, "Tried to start a worker that was already started");
|
||||||
@@ -137,6 +143,7 @@ namespace BlackMisc
|
|||||||
|
|
||||||
moveToThread(thread);
|
moveToThread(thread);
|
||||||
connect(thread, &QThread::started, this, &CContinuousWorker::initialize);
|
connect(thread, &QThread::started, this, &CContinuousWorker::initialize);
|
||||||
|
connect(thread, &QThread::finished, &m_updateTimer, &QTimer::stop);
|
||||||
connect(thread, &QThread::finished, this, &CContinuousWorker::cleanup);
|
connect(thread, &QThread::finished, this, &CContinuousWorker::cleanup);
|
||||||
connect(thread, &QThread::finished, this, &CContinuousWorker::finish);
|
connect(thread, &QThread::finished, this, &CContinuousWorker::finish);
|
||||||
thread->start(priority);
|
thread->start(priority);
|
||||||
@@ -146,6 +153,7 @@ namespace BlackMisc
|
|||||||
{
|
{
|
||||||
Q_ASSERT_X(!CThreadUtils::isApplicationThreadObjectThread(this), Q_FUNC_INFO, "Try to stop main thread");
|
Q_ASSERT_X(!CThreadUtils::isApplicationThreadObjectThread(this), Q_FUNC_INFO, "Try to stop main thread");
|
||||||
setEnabled(false);
|
setEnabled(false);
|
||||||
|
// remark: cannot stop timer here, as I am normally not in the correct thread
|
||||||
thread()->quit();
|
thread()->quit();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -158,6 +166,36 @@ namespace BlackMisc
|
|||||||
ownThread->wait();
|
ownThread->wait();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CContinuousWorker::startUpdating(int updateTimeSecs)
|
||||||
|
{
|
||||||
|
Q_ASSERT_X(hasStarted(), Q_FUNC_INFO, "Worker not yet started");
|
||||||
|
if (!CThreadUtils::isCurrentThreadObjectThread(this))
|
||||||
|
{
|
||||||
|
// shift in correct thread
|
||||||
|
if (!this->isFinished())
|
||||||
|
{
|
||||||
|
QTimer::singleShot(0, this, [this, updateTimeSecs]
|
||||||
|
{
|
||||||
|
if (this->isFinished()) { return; }
|
||||||
|
this->startUpdating(updateTimeSecs);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// here in correct timer thread
|
||||||
|
if (updateTimeSecs < 0)
|
||||||
|
{
|
||||||
|
setEnabled(false);
|
||||||
|
m_updateTimer.stop();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
setEnabled(true);
|
||||||
|
m_updateTimer.start(1000 * updateTimeSecs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void CContinuousWorker::finish()
|
void CContinuousWorker::finish()
|
||||||
{
|
{
|
||||||
setFinished();
|
setFinished();
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ namespace BlackMisc
|
|||||||
template <typename F>
|
template <typename F>
|
||||||
void singleShot(int msec, QObject *target, F &&task)
|
void singleShot(int msec, QObject *target, F &&task)
|
||||||
{
|
{
|
||||||
QSharedPointer<QTimer> timer(new QTimer, [](QObject *o) { QMetaObject::invokeMethod(o, "deleteLater"); });
|
QSharedPointer<QTimer> timer(new QTimer, [](QObject * o) { QMetaObject::invokeMethod(o, "deleteLater"); });
|
||||||
timer->setSingleShot(true);
|
timer->setSingleShot(true);
|
||||||
timer->moveToThread(target->thread());
|
timer->moveToThread(target->thread());
|
||||||
QObject::connect(timer.data(), &QTimer::timeout, target, [trace = getStackTrace(), task = std::forward<F>(task), timer]() mutable
|
QObject::connect(timer.data(), &QTimer::timeout, target, [trace = getStackTrace(), task = std::forward<F>(task), timer]() mutable
|
||||||
@@ -75,7 +75,7 @@ namespace BlackMisc
|
|||||||
CRegularThread(QObject *parent = nullptr) : QThread(parent) {}
|
CRegularThread(QObject *parent = nullptr) : QThread(parent) {}
|
||||||
|
|
||||||
//! Destructor
|
//! Destructor
|
||||||
~CRegularThread();
|
virtual ~CRegularThread();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
//! \copydoc QThread::run
|
//! \copydoc QThread::run
|
||||||
@@ -149,7 +149,8 @@ namespace BlackMisc
|
|||||||
void doIfFinishedElse(F1 ifFunctor, F2 elseFunctor) const
|
void doIfFinishedElse(F1 ifFunctor, F2 elseFunctor) const
|
||||||
{
|
{
|
||||||
QMutexLocker lock(&m_finishedMutex);
|
QMutexLocker lock(&m_finishedMutex);
|
||||||
if (m_finished) { ifFunctor(); } else { elseFunctor(); }
|
if (m_finished) { ifFunctor(); }
|
||||||
|
else { elseFunctor(); }
|
||||||
}
|
}
|
||||||
|
|
||||||
//! Blocks until the task is finished.
|
//! Blocks until the task is finished.
|
||||||
@@ -275,7 +276,7 @@ namespace BlackMisc
|
|||||||
* \param owner Will be the parent of the new thread (the worker has no parent).
|
* \param owner Will be the parent of the new thread (the worker has no parent).
|
||||||
* \param name A name for the worker, which will be used to create a name for the thread.
|
* \param name A name for the worker, which will be used to create a name for the thread.
|
||||||
*/
|
*/
|
||||||
CContinuousWorker(QObject *owner, const QString &name = "") : m_owner(owner), m_name(name) {}
|
CContinuousWorker(QObject *owner, const QString &name = "");
|
||||||
|
|
||||||
//! Starts a thread and moves the worker into it.
|
//! Starts a thread and moves the worker into it.
|
||||||
void start(QThread::Priority priority = QThread::InheritPriority);
|
void start(QThread::Priority priority = QThread::InheritPriority);
|
||||||
@@ -297,6 +298,10 @@ namespace BlackMisc
|
|||||||
//! \threadsafe
|
//! \threadsafe
|
||||||
void setEnabled(bool enabled) { m_enabled = enabled; }
|
void setEnabled(bool enabled) { m_enabled = enabled; }
|
||||||
|
|
||||||
|
//! Start updating (start/stop timer)
|
||||||
|
//! \threadsafe
|
||||||
|
void startUpdating(int updateTimeSecs);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
//! Called when the thread is started.
|
//! Called when the thread is started.
|
||||||
virtual void initialize() {}
|
virtual void initialize() {}
|
||||||
@@ -307,6 +312,8 @@ namespace BlackMisc
|
|||||||
//! Name of the worker
|
//! Name of the worker
|
||||||
const QString &getName() { return m_name; }
|
const QString &getName() { return m_name; }
|
||||||
|
|
||||||
|
QTimer m_updateTimer { this }; //!< timer which can be used by implementing classes
|
||||||
|
|
||||||
private:
|
private:
|
||||||
//! Called after cleanup().
|
//! Called after cleanup().
|
||||||
void finish();
|
void finish();
|
||||||
|
|||||||
Reference in New Issue
Block a user