Comit helper-classes for now

This commit is contained in:
Gerhard Hoffmann 2023-06-16 16:47:13 +02:00
parent 60f845aa8d
commit e2d9c5aa90
4 changed files with 126 additions and 0 deletions

66
worker.cpp Normal file
View File

@ -0,0 +1,66 @@
#include "worker.h"
#include "update.h"
#include <QCoreApplication>
#include <QApplication>
#include <QDebug>
#include <QTimer>
#include <QFileInfo>
#include <QDir>
#include <QThread>
#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();
}

30
worker.h Normal file
View File

@ -0,0 +1,30 @@
#ifndef WORKER_H_INCLUDED
#define WORKER_H_INCLUDED
#include <QObject>
#include <QString>
#include <QTimer>
#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

13
worker_thread.cpp Normal file
View File

@ -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() {
//
//}

17
worker_thread.h Normal file
View File

@ -0,0 +1,17 @@
#ifndef WORKER_THREAD_H_INCLUDED
#define WORKER_THREAD_H_INCLUDED
#include <QThread>
#include <QString>
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