Refined close

This commit is contained in:
Indy970 2019-09-21 18:00:43 +02:00
parent 8390266f74
commit 0af9b39b3b
6 changed files with 86 additions and 52 deletions

View file

@ -1,5 +1,4 @@
#include "eventthread.h"
#include "mainwindow.h"
EventThread::EventThread(MainWindow * parent)
@ -9,10 +8,14 @@ EventThread::EventThread(MainWindow * parent)
hpapi=main->getAPI();
}
void EventThread::timerEvent()
void EventThread::timerAction()
{
if(hpapi) {
QMutexLocker locker(&mutex);
// qDebug()<<"In Eventhandler";
// QThread::msleep(1);
if(hpapi) {
hpapi->eventHandler();
}
}
@ -20,22 +23,27 @@ void EventThread::timerEvent()
void EventThread::start() {
timer = new QTimer(this);
timer->setInterval(100);
timer->connect(timer, SIGNAL(timeout()), this, SLOT(timerEvent()));
// timer->callOnTimeout(SLOT(timerEvent()),Qt::AutoConnection);
timer->connect(timer, SIGNAL(timeout()), this, SLOT(timerAction()),Qt::AutoConnection);
timer->connect(this, SIGNAL(stop()), this, SLOT(stopTimer()));
timer->start();
qDebug()<<"EventThread::started Timer";
}
void EventThread::exit() {
emit stop();
stop();
}
void EventThread::stopTimer() {
qDebug()<<"EventThread::stop Timer";
timer->stop();
emit stopped();
}
EventThread::~EventThread()
{
stopTimer();
if (timer!=nullptr) {
delete timer;
timer=nullptr;

View file

@ -2,6 +2,7 @@
#define EVENTTHREAD_H
#include <QThread>
#include <QTimer>
#include <QMutex>
#include "hpusb.h"
class MainWindow;
@ -13,17 +14,20 @@ private:
MainWindow * main;
hpusb * hpapi;
QTimer * timer=nullptr;
mutable QMutex mutex;
public:
EventThread(MainWindow * parent);
~EventThread();
public slots:
void timerEvent();
void timerAction();
void start();
void exit();
void stopTimer();
signals:
void stop();
void stopped();
};
#endif // EVENTTHREAD_H

View file

@ -1389,6 +1389,7 @@ int hpusb::hotplugcallback(struct libusb_context *ctx, struct libusb_device *dev
//eventhandler called periodically to handle async events
int hpusb::eventHandler() {
// QMutexLocker locker(&mutex);
int completed;
// qDebug()<<"In Eventhandler";
libusb_handle_events_completed(nullptr, &completed);
@ -1397,6 +1398,7 @@ int hpusb::eventHandler() {
int hpusb::hp_close() {
qDebug()<<"hpusb::close";
libusb_device_handle * usbhandle;
usbhandle=hp_handle.usbhandle;
libusb_close(usbhandle);
@ -1406,6 +1408,7 @@ int hpusb::hp_close() {
}
hpusb::~hpusb() {
hp_close();
libusb_exit(ctx);
qDebug()<<"hpusb::close";
qDebug()<<"hpusb:: delete";
}

View file

@ -4,6 +4,7 @@
#include <libusb.h>
#include <QObject>
#include <QByteArray>
#include <QMutex>
struct hp_Settings;
class hpCalcData;
@ -160,7 +161,7 @@ private:
out
} exitflag;
libusb_context * ctx;
libusb_context * ctx = nullptr;
hp_Handle hp_handle;
libusb_hotplug_callback_handle hp_callback_handle;
@ -168,12 +169,14 @@ private:
int lb_init=0;
// OUT-going transfers (OUT from host PC to USB-device)
struct libusb_transfer *transfer_out = NULL;
struct libusb_transfer *transfer_out = nullptr;
// IN-coming transfers (IN to host PC from USB-device)
struct libusb_transfer *transfer_in = NULL;
struct libusb_transfer *transfer_in = nullptr;
int do_exit = 0;
QMutex mutex;
public:
hpusb();

View file

@ -96,7 +96,7 @@ MainWindow::MainWindow(QWidget *parent) :
connect(ui->actionOpen,SIGNAL(triggered()),this,SLOT(onOpen()));
connect(ui->actionAbout_HP_Connect,SIGNAL(triggered()),this,SLOT(about()));
connect(ui->actionExit,SIGNAL(triggered()),this,SLOT(exit()));
// connect(ui->actionExit,SIGNAL(triggered()),this,SLOT(exit()));
connect(ui->actionContent,SIGNAL(triggered()),this,SLOT(showContent()));
connect(ui->actionCalculators,SIGNAL(triggered()),this,SLOT(showCalculator()));
connect(ui->actionMessages,SIGNAL(triggered()),this,SLOT(showMessage()));
@ -124,6 +124,9 @@ MainWindow::MainWindow(QWidget *parent) :
eventTimer->moveToThread(eventThread);
// connect(eventTimer,SIGNAL(timeout()),this,SLOT(eventHandler()));
connect(eventThread,SIGNAL(started()),eventTimer,SLOT(start()));
connect(eventTimer,SIGNAL(stopped()),this,SLOT(setTimerStopped()));
connect(this,SIGNAL(stopTimer()),eventTimer,SLOT(stopTimer()));
eventThread->start();
ui->dwMessenger->hide();
@ -484,54 +487,25 @@ void MainWindow::monitorAddImage(hp_ScreenShot scrnshot) {
ui->dwMonitor->show();
}
void MainWindow::setTimerStopped() {
qDebug()<<"MainWindow:: set timerStopped Flag";
timerStopped=1;
}
void MainWindow::closeEvent(QCloseEvent *event)
{
qDebug()<<"MainWindow:: closeEvent Step 1";
writeSettings();
//stop the timer pulse
emit stopTimer();
event->accept();
if (eventThread!=nullptr) {
eventThread->quit();
eventThread->wait(100);
}
qDebug()<<"MainWindow:: closeEvent Step 2";
ui->tvCalculators->close();
ui->tvContent->close();
ui->dwContent->close();
ui->dwMonitor->close();
ui->dwMessenger->close();
ui->dwCalculator->close();
qDebug()<<"MainWindow:: closeEvent Step 3";
if (main_err!=nullptr) {
delete main_err;
main_err=nullptr;
}
/*
if (treeMenu!=nullptr) {
delete treeMenu;
treeMenu=nullptr;
}
*/
if (myModel!=nullptr) {
delete myModel;
myModel=nullptr;
}
if (hpapi!=nullptr) {
delete hpapi;
hpapi=nullptr;
}
qDebug()<<"MainWindow:: closeEvent Step 4";
QThread::msleep(100);
close();
qDebug()<<"MainWindow:: closeEvent Step 5";
qDebug()<<"MainWindow:: closeEvent Step 2";
}
void MainWindow::writeSettings()
@ -665,5 +639,40 @@ void MainWindow::on_tvCalculators_customContextMenuRequested(const QPoint &pos)
//destructor
MainWindow::~MainWindow()
{
qDebug()<<"MainWindow:: closeEvent Step 3";
ui->tvCalculators->close();
ui->tvContent->close();
ui->dwContent->close();
ui->dwMonitor->close();
ui->dwMessenger->close();
ui->dwCalculator->close();
qDebug()<<"MainWindow:: closeEvent Step 4";
if (main_err!=nullptr) {
delete main_err;
main_err=nullptr;
}
if (myModel!=nullptr) {
delete myModel;
myModel=nullptr;
}
qDebug()<<"MainWindow:: closeEvent Step 5";
//might need a mechanism to inform eventThread that the comms module is closed
if ((hpapi!=nullptr) && (timerStopped)) {
delete hpapi;
hpapi=nullptr;
}
//delete extra thread
if (eventThread!=nullptr) {
eventThread->quit();
}
qDebug()<<"MainWindow:: closing";
}

View file

@ -42,6 +42,9 @@ public:
void createActions();
hpusb * getAPI();
signals:
void stopTimer();
private slots:
void onOpen();
void onTestSettings();
@ -61,6 +64,7 @@ private slots:
void testFunction();
void writeSettings();
void readSettings();
void setTimerStopped();
treeModel * getTreeModel();
void treeMenuAction(bool);
@ -69,6 +73,7 @@ private slots:
void refresh(bool clicked);
void hotplug_handler(int );
private:
QString lastCalc;
@ -91,6 +96,8 @@ private:
void setContentWindow();
void setTreeMenu();
void monitorAddImage(hp_ScreenShot scrnshot);
int timerStopped=0; //flag
};
#endif // MAINWINDOW_H
#endif
//MAINWINDOW_H