diff --git a/worker.cpp b/worker.cpp new file mode 100644 index 0000000..73c0c16 --- /dev/null +++ b/worker.cpp @@ -0,0 +1,66 @@ +#include "worker.h" +#include "update.h" + +#include +#include +#include +#include +#include +#include +#include + +#include "message_handler.h" +#include "plugins/interfaces.h" + + +Worker::Worker(QString update_ctrl_file, QString workingDir) + : m_update_ctrl_file(update_ctrl_file) + , m_workingDir(workingDir) + , m_workerThread("workerThread") { + + this->moveToThread(&m_workerThread); + m_workerThread.start(); + QThread::usleep(100000); + + int cnt = 0; + while (!m_workerThread.isRunning()) { + if (++cnt > 5) { + qCritical() << "starting worker thread FAILED"; + return; + } + QThread::sleep(1); + } + + connect(this, SIGNAL(workNow()), this, SLOT(work()), Qt::QueuedConnection); + connect(&m_timer, SIGNAL(timeout()), this, SLOT(update())); + m_timer.setSingleShot(true); + m_timer.start(1000); +} + +Worker::~Worker() { + int cnt = 0; + m_workerThread.quit(); + while (!m_workerThread.isFinished()) { + if (!m_workerThread.wait(1000)) { + if (++cnt > 5) { + qCritical() << "stopping worker thread FAILED"; + return; + } + } + } +} + +void Worker::update() { + qCritical() << __func__ << ":" << __LINE__; + emit workNow(); +} + +void Worker::work() { + qCritical() << __func__ << ":" << __LINE__; + //Update m_update(m_update_ctrl_file, m_workingDir); + QThread::sleep(3); + //if (m_update.doUpdate()) { + //} + m_workerThread.quit(); + QApplication::quit(); +} diff --git a/worker.h b/worker.h new file mode 100644 index 0000000..ced641e --- /dev/null +++ b/worker.h @@ -0,0 +1,30 @@ +#ifndef WORKER_H_INCLUDED +#define WORKER_H_INCLUDED + +#include +#include +#include + +#include "worker_thread.h" + +class Worker : public QObject { + Q_OBJECT + + QString m_update_ctrl_file; + QString m_workingDir; + WorkerThread m_workerThread; + QTimer m_timer; +public: + explicit Worker(QString update_ctrl_file, QString workingDir); + ~Worker(); + void quit() { return m_workerThread.quit(); } + +signals: + void workNow(); + +public slots: + void work(); + void update(); +}; + +#endif // WORKER_H_INCLUDED diff --git a/worker_thread.cpp b/worker_thread.cpp new file mode 100644 index 0000000..b36e8a8 --- /dev/null +++ b/worker_thread.cpp @@ -0,0 +1,13 @@ +#include "worker_thread.h" + +WorkerThread::WorkerThread(QString const &name, + QObject *parent) : QThread(parent) { + this->setObjectName(name); +} + +WorkerThread::~WorkerThread() { +} + +//void WorkerThread::run() { +// +//} diff --git a/worker_thread.h b/worker_thread.h new file mode 100644 index 0000000..b15a509 --- /dev/null +++ b/worker_thread.h @@ -0,0 +1,17 @@ +#ifndef WORKER_THREAD_H_INCLUDED +#define WORKER_THREAD_H_INCLUDED + +#include +#include + +class WorkerThread : public QThread { + Q_OBJECT +public: + WorkerThread(QString const &name, QObject *parent = nullptr); + virtual ~WorkerThread(); + +protected: + // virtual void run(); +}; + +#endif // WORKER_THREAD_H_INCLUDED