Ref T730, use an event loop with parent "this"

* central function to get QEventLoop (this can also be used to set a timeout)
* do NOT use sApp as parent -> parent must be in same thread
This commit is contained in:
Klaus Basan
2019-10-06 17:29:11 +02:00
committed by Mat Sutcliffe
parent e4e4dd090e
commit b802933422
4 changed files with 20 additions and 7 deletions

View File

@@ -61,7 +61,7 @@ namespace BlackCore
{"networkversion", networkVersion.toString()},
};
QPointer<QEventLoop> loop(new QEventLoop(sApp));
QPointer<QEventLoop> loop(this->newEventLoop());
QNetworkRequest request(url);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
@@ -161,7 +161,7 @@ namespace BlackCore
{
if (isShuttingDown()) { return {}; }
QPointer<QEventLoop> loop(new QEventLoop(sApp));
QPointer<QEventLoop> 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<QEventLoop> loop(new QEventLoop(sApp));
QPointer<QEventLoop> 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();

View File

@@ -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();

View File

@@ -72,7 +72,7 @@ namespace BlackMisc
return QStringLiteral("0x%1").arg(reinterpret_cast<long long>(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"); }

View File

@@ -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();