diff --git a/src/blackcore/afv/connection/apiserverconnection.cpp b/src/blackcore/afv/connection/apiserverconnection.cpp index e656ce95d..c25b40c13 100644 --- a/src/blackcore/afv/connection/apiserverconnection.cpp +++ b/src/blackcore/afv/connection/apiserverconnection.cpp @@ -61,7 +61,7 @@ namespace BlackCore {"networkversion", networkVersion.toString()}, }; - QPointer loop(new QEventLoop(sApp)); + QPointer loop(this->newEventLoop()); QNetworkRequest request(url); request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json"); @@ -161,7 +161,7 @@ namespace BlackCore { if (isShuttingDown()) { return {}; } - QPointer loop(new QEventLoop(sApp)); + QPointer loop(this->newEventLoop()); QByteArray receivedData; // posted in QAM thread, reply is nullptr if called from another thread @@ -197,7 +197,7 @@ namespace BlackCore { if (isShuttingDown()) { return {}; } - QPointer loop(new QEventLoop(sApp)); + QPointer loop(this->newEventLoop()); QByteArray receivedData; // posted in QAM thread, reply is nullptr if called from another thread @@ -295,7 +295,7 @@ namespace BlackCore { if (QDateTime::currentDateTimeUtc() > m_expiryLocalUtc.addSecs(-5 * 60)) { - connectTo(m_username, m_password, m_networkVersion); + this->connectTo(m_username, m_password, m_networkVersion); } } @@ -327,6 +327,16 @@ namespace BlackCore } } + QEventLoop *CApiServerConnection::newEventLoop() + { + QEventLoop *loop = new QEventLoop(this); + if (sApp) + { + QObject::connect(sApp, &CApplication::aboutToShutdown, loop, &QEventLoop::quit, Qt::QueuedConnection); + } + return loop; + } + bool CApiServerConnection::isShuttingDown() { return !sApp || sApp->isShuttingDown(); diff --git a/src/blackcore/afv/connection/apiserverconnection.h b/src/blackcore/afv/connection/apiserverconnection.h index 9262049bd..ae3c7ab94 100644 --- a/src/blackcore/afv/connection/apiserverconnection.h +++ b/src/blackcore/afv/connection/apiserverconnection.h @@ -101,7 +101,7 @@ namespace BlackCore { if (! m_isAuthenticated) { - BlackMisc::CLogMessage(this).debug(u"AFV not authenticated"); + CLogMessage(this).debug(u"AFV not authenticated"); return {}; } @@ -149,6 +149,9 @@ namespace BlackCore //! Message if reply has error void logRequestDuration(const QNetworkReply *reply, const QString &addMsg = {}); + //! Get QLoop for network access, using class must delete the loop + QEventLoop *newEventLoop(); + //! Application shutting down static bool isShuttingDown(); diff --git a/src/blackmisc/threadutils.cpp b/src/blackmisc/threadutils.cpp index 5af2ebd1d..2a05bda42 100644 --- a/src/blackmisc/threadutils.cpp +++ b/src/blackmisc/threadutils.cpp @@ -72,7 +72,7 @@ namespace BlackMisc return QStringLiteral("0x%1").arg(reinterpret_cast(t), 0, 16); } - const QString CThreadUtils::threadInfo(QThread *thread) + const QString CThreadUtils::threadInfo(const QThread *thread) { static const QString info("thread: %1 name: '%2' priority: '%3'"); if (!thread) { return QString("no thread"); } diff --git a/src/blackmisc/threadutils.h b/src/blackmisc/threadutils.h index c8030de06..f99fabf60 100644 --- a/src/blackmisc/threadutils.h +++ b/src/blackmisc/threadutils.h @@ -48,7 +48,7 @@ namespace BlackMisc static const QString threadToString(const void *t); //! Info about current thread - static const QString threadInfo(QThread *thread); + static const QString threadInfo(const QThread *thread); //! Info about current thread static const QString currentThreadInfo();