cutter/src/utils/AsyncTask.h

59 lines
956 B
C
Raw Normal View History

2018-05-26 16:21:23 +00:00
#ifndef ASYNCTASK_H
#define ASYNCTASK_H
#include <QRunnable>
#include <QThreadPool>
2018-05-26 18:09:20 +00:00
#include <QMutex>
class AsyncTaskManager;
2018-05-26 16:21:23 +00:00
class AsyncTask : public QObject, public QRunnable
{
Q_OBJECT
2018-05-26 18:09:20 +00:00
friend class AsyncTaskManager;
2018-05-26 16:21:23 +00:00
public:
2018-05-26 18:09:20 +00:00
AsyncTask(QObject *parent = nullptr);
~AsyncTask();
2018-05-26 16:21:23 +00:00
void run() override final;
2018-05-26 18:09:20 +00:00
void wait();
bool wait(int timeout);
virtual void interrupt();
bool isInterrupted() { return interrupted; }
bool isRunning() { return running; }
protected:
2018-05-26 16:21:23 +00:00
virtual void runTask() =0;
signals:
void finished();
2018-05-26 18:09:20 +00:00
private:
bool running;
bool interrupted;
QMutex runningMutex;
void prepareRun();
2018-05-26 16:21:23 +00:00
};
class AsyncTaskManager : public QObject
{
Q_OBJECT
private:
QThreadPool *threadPool;
public:
explicit AsyncTaskManager(QObject *parent = nullptr);
~AsyncTaskManager();
void start(AsyncTask *task);
};
#endif //ASYNCTASK_H