#ifndef WORKER_H
#define WORKER_H
#include <QObject>
#include <QMutex>
class workerThread;
{
Q_OBJECT
public:
explicit Worker
(QObject *parent
= 0);
void requestWork(int id);
void abort();
void run();
void runThread(int id);
private:
bool _abort;
bool _working;
int ID;
bool prepareThread(workerThread *&thread);
workerThread *thisThread;
signals:
void workRequested();
void valueChanged
(const QString &value
);
void finished();
public slots:
void doWork();
void thisThreadStarted();
void thisThreadFinished();
};
#include <QThread>
class workerThread
: public QThread{
Q_OBJECT
public:
workerThread(Worker *workerThr):workerThr(workerThr){}
virtual ~workerThread(){}
void run() {workerThr->run();}
protected:
Worker *workerThr;
};
#endif // WORKER_H
#ifndef WORKER_H
#define WORKER_H
#include <QObject>
#include <QMutex>
class workerThread;
class Worker : public QObject
{
Q_OBJECT
public:
explicit Worker(QObject *parent = 0);
void requestWork(int id);
void abort();
void run();
void runThread(int id);
private:
bool _abort;
bool _working;
QMutex mutex;
int ID;
bool prepareThread(workerThread *&thread);
workerThread *thisThread;
signals:
void workRequested();
void valueChanged(const QString &value);
void finished();
public slots:
void doWork();
void thisThreadStarted();
void thisThreadFinished();
};
#include <QThread>
class workerThread : public QThread
{
Q_OBJECT
public:
workerThread(Worker *workerThr):workerThr(workerThr){}
virtual ~workerThread(){}
void run() {workerThr->run();}
protected:
Worker *workerThr;
};
#endif // WORKER_H
To copy to clipboard, switch view to plain text mode
#include "worker.h"
#include <QTimer>
#include <QEventLoop>
#include <QThread>
#include <QDebug>
{
_working =false;
_abort = false;
thisThread = NULL;
}
void Worker::abort()
{
mutex.lock();
if (_working)
{
_abort = true;
//qDebug()<<"Abort "<<QString::number(ID);
}
mutex.unlock();
}
void Worker::run()
{
doWork();
}
void Worker::runThread(int id)
{
//abort thread
abort();
//check if thread still run
//check thread every 10ms
while(thisThread)
{
QTimer::singleShot(10,
&loop,
SLOT(quit
()));
loop.exec();
}
mutex.lock();
_working = true;
_abort = false;
ID = id;
mutex.unlock();
//run the new thread
thisThread = NULL;
prepareThread(thisThread);
thisThread->start();
}
void Worker::doWork()
{
qDebug()<<"Process "<<QString::number(ID);
for (int i = 0; i < 10; i ++)
{
// Checks if the process should be aborted
mutex.lock();
bool abort = _abort;
mutex.unlock();
if (abort)
{
qDebug()<<"Aborting "<<QString::number(ID);
break;
}
else
{
// add very heavy process
QTimer::singleShot(200,
&loop,
SLOT(quit
()));
loop.exec();
}
}
// Set _working to false, meaning the process can't be aborted anymore.
mutex.lock();
_working = false;
mutex.unlock();
qDebug()<<"Finished "<<QString::number(ID);
emit finished();
}
bool Worker::prepareThread(workerThread *&thread)
{
thread = new workerThread(this);
connect(thread, SIGNAL(started()), this,
SLOT(thisThreadStarted()), Qt::BlockingQueuedConnection);
connect(thread, SIGNAL(finished()), this,
SLOT(thisThreadFinished()), Qt::BlockingQueuedConnection);
connect(thread, SIGNAL(terminated()), this,
SLOT(thisThreadFinished()), Qt::BlockingQueuedConnection);
return true;
}
void Worker::thisThreadStarted()
{
}
void Worker::thisThreadFinished()
{
thisThread = NULL;
}
#include "worker.h"
#include <QTimer>
#include <QEventLoop>
#include <QThread>
#include <QDebug>
Worker::Worker(QObject *parent) :
QObject(parent)
{
_working =false;
_abort = false;
thisThread = NULL;
}
void Worker::abort()
{
mutex.lock();
if (_working)
{
_abort = true;
//qDebug()<<"Abort "<<QString::number(ID);
}
mutex.unlock();
}
void Worker::run()
{
doWork();
}
void Worker::runThread(int id)
{
//abort thread
abort();
//check if thread still run
//check thread every 10ms
while(thisThread)
{
QEventLoop loop;
QTimer::singleShot(10, &loop, SLOT(quit()));
loop.exec();
}
mutex.lock();
_working = true;
_abort = false;
ID = id;
mutex.unlock();
//run the new thread
thisThread = NULL;
prepareThread(thisThread);
thisThread->start();
}
void Worker::doWork()
{
qDebug()<<"Process "<<QString::number(ID);
for (int i = 0; i < 10; i ++)
{
// Checks if the process should be aborted
mutex.lock();
bool abort = _abort;
mutex.unlock();
if (abort)
{
qDebug()<<"Aborting "<<QString::number(ID);
break;
}
else
{
// add very heavy process
QEventLoop loop;
QTimer::singleShot(200, &loop, SLOT(quit()));
loop.exec();
}
}
// Set _working to false, meaning the process can't be aborted anymore.
mutex.lock();
_working = false;
mutex.unlock();
qDebug()<<"Finished "<<QString::number(ID);
emit finished();
}
bool Worker::prepareThread(workerThread *&thread)
{
thread = new workerThread(this);
connect(thread, SIGNAL(started()), this,
SLOT(thisThreadStarted()), Qt::BlockingQueuedConnection);
connect(thread, SIGNAL(finished()), this,
SLOT(thisThreadFinished()), Qt::BlockingQueuedConnection);
connect(thread, SIGNAL(terminated()), this,
SLOT(thisThreadFinished()), Qt::BlockingQueuedConnection);
return true;
}
void Worker::thisThreadStarted()
{
}
void Worker::thisThreadFinished()
{
thisThread = NULL;
}
To copy to clipboard, switch view to plain text mode
Bookmarks