Use QSharedPointer for AsyncTask

This commit is contained in:
Florian Märkl 2018-05-28 16:19:04 +02:00
parent 7777707062
commit 14cfe74324
7 changed files with 46 additions and 32 deletions

View File

@ -6,8 +6,8 @@
#include <QDebug>
#include <QCheckBox>
AnalTask::AnalTask(QObject *parent) :
AsyncTask(parent)
AnalTask::AnalTask() :
AsyncTask()
{
}

View File

@ -44,7 +44,7 @@ class AnalTask : public AsyncTask
Q_OBJECT
public:
explicit AnalTask(QObject *parent = nullptr);
explicit AnalTask();
~AnalTask();
QString getTitle() override { return tr("Initial Analysis"); }

View File

@ -5,10 +5,10 @@
#include "ui_AsyncTaskDialog.h"
AsyncTaskDialog::AsyncTaskDialog(AsyncTask *task, QWidget *parent)
AsyncTaskDialog::AsyncTaskDialog(AsyncTask::Ptr task, QWidget *parent)
: QDialog(parent),
ui(new Ui::AsyncTaskDialog),
task(task)
task(task),
ui(new Ui::AsyncTaskDialog)
{
ui->setupUi(this);
@ -17,9 +17,8 @@ AsyncTaskDialog::AsyncTaskDialog(AsyncTask *task, QWidget *parent)
setWindowTitle(title);
}
connect(task, &AsyncTask::logChanged, this, &AsyncTaskDialog::updateLog);
connect(task, &AsyncTask::finished, this, [this]() {
this->task = nullptr;
connect(task.data(), &AsyncTask::logChanged, this, &AsyncTaskDialog::updateLog);
connect(task.data(), &AsyncTask::finished, this, [this]() {
close();
});
@ -44,10 +43,6 @@ void AsyncTaskDialog::updateLog(const QString &log)
void AsyncTaskDialog::updateProgressTimer()
{
if(!task) {
return;
}
int secondsElapsed = (task->getTimer().elapsed() + 500) / 1000;
int minutesElapsed = secondsElapsed / 60;
int hoursElapsed = minutesElapsed / 60;
@ -67,7 +62,7 @@ void AsyncTaskDialog::updateProgressTimer()
void AsyncTaskDialog::closeEvent(QCloseEvent *event)
{
if (interruptOnClose && task) {
if (interruptOnClose) {
task->interrupt();
task->wait();
}
@ -77,7 +72,5 @@ void AsyncTaskDialog::closeEvent(QCloseEvent *event)
void AsyncTaskDialog::reject()
{
if (task) {
task->interrupt();
}
}

View File

@ -7,18 +7,18 @@
#include <QDialog>
#include <QTimer>
#include "utils/AsyncTask.h"
namespace Ui {
class AsyncTaskDialog;
}
class AsyncTask;
class AsyncTaskDialog : public QDialog
{
Q_OBJECT
public:
AsyncTaskDialog(AsyncTask *task, QWidget *parent = nullptr);
AsyncTaskDialog(AsyncTask::Ptr task, QWidget *parent = nullptr);
~AsyncTaskDialog();
public slots:
@ -33,7 +33,7 @@ protected:
private:
std::unique_ptr<Ui::AsyncTaskDialog> ui;
AsyncTask *task;
AsyncTask::Ptr task;
QTimer timer;
bool interruptOnClose;

View File

@ -1,3 +1,6 @@
#include "utils/AsyncTask.h"
#include "OptionsDialog.h"
#include "ui_OptionsDialog.h"
#include "MainWindow.h"
@ -231,15 +234,17 @@ void OptionsDialog::setupAndStartAnalysis(int level, QList<QString> advanced)
}
AnalTask *analTask = new AnalTask(main);
AnalTask *analTask = new AnalTask();
analTask->setOptions(options);
connect(analTask, &AnalTask::openFileFailed, main, &MainWindow::openNewFileFailed);
connect(analTask, &AsyncTask::finished, main, &MainWindow::finalizeOpen);
Core()->getAsyncTaskManager()->start(analTask);
AsyncTask::Ptr analTaskPtr(analTask);
AsyncTaskDialog *taskDialog = new AsyncTaskDialog(analTask);
Core()->getAsyncTaskManager()->start(analTaskPtr);
AsyncTaskDialog *taskDialog = new AsyncTaskDialog(analTaskPtr);
taskDialog->setAttribute(Qt::WA_DeleteOnClose);
taskDialog->show();

View File

@ -1,11 +1,11 @@
#include "AsyncTask.h"
AsyncTask::AsyncTask(QObject *parent)
: QObject(parent),
AsyncTask::AsyncTask()
: QObject(nullptr),
QRunnable()
{
setAutoDelete(true);
setAutoDelete(false);
running = false;
}
@ -44,12 +44,17 @@ void AsyncTask::prepareRun()
void AsyncTask::run()
{
runningMutex.lock();
running = true;
logBuffer = "";
emit logChanged(logBuffer);
runTask();
emit finished();
running = false;
emit finished();
runningMutex.unlock();
}
@ -69,8 +74,14 @@ AsyncTaskManager::~AsyncTaskManager()
{
}
void AsyncTaskManager::start(AsyncTask *task)
void AsyncTaskManager::start(AsyncTask::Ptr task)
{
tasks.append(task);
task->prepareRun();
threadPool->start(task);
QWeakPointer<AsyncTask> weakPtr = task;
connect(task.data(), &AsyncTask::finished, this, [this, weakPtr]() {
tasks.removeOne(weakPtr);
});
threadPool->start(task.data());
}

View File

@ -6,6 +6,8 @@
#include <QThreadPool>
#include <QMutex>
#include <QElapsedTimer>
#include <QSharedPointer>
#include <QList>
class AsyncTaskManager;
@ -16,7 +18,9 @@ class AsyncTask : public QObject, public QRunnable
friend class AsyncTaskManager;
public:
AsyncTask(QObject *parent = nullptr);
using Ptr = QSharedPointer<AsyncTask>;
AsyncTask();
~AsyncTask();
void run() override final;
@ -59,12 +63,13 @@ class AsyncTaskManager : public QObject
private:
QThreadPool *threadPool;
QList<AsyncTask::Ptr> tasks;
public:
explicit AsyncTaskManager(QObject *parent = nullptr);
~AsyncTaskManager();
void start(AsyncTask *task);
void start(AsyncTask::Ptr task);
};