From 2bd9120c1c9b919d35ab31066503fadc4f61752d Mon Sep 17 00:00:00 2001 From: Roland Winklmeier Date: Thu, 12 Jan 2017 22:56:35 +0100 Subject: [PATCH] Quit and wait for GFS parser thread to be finished before closing refs #854 --- .../weatherdata/gfs/weatherdatagfs.cpp | 20 ++++++++++++++----- src/plugins/weatherdata/gfs/weatherdatagfs.h | 7 +++++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/plugins/weatherdata/gfs/weatherdatagfs.cpp b/src/plugins/weatherdata/gfs/weatherdatagfs.cpp index 7a2ce2100..7942c0e17 100644 --- a/src/plugins/weatherdata/gfs/weatherdatagfs.cpp +++ b/src/plugins/weatherdata/gfs/weatherdatagfs.cpp @@ -9,7 +9,6 @@ #include "weatherdatagfs.h" #include "blackcore/application.h" -#include "blackmisc/worker.h" #include "blackmisc/logmessage.h" #include "blackmisc/math/mathutils.h" #include "blackmisc/geo/coordinategeodetic.h" @@ -55,6 +54,11 @@ namespace BlackWxPlugin IWeatherData(parent) { } + CWeatherDataGfs::~CWeatherDataGfs() + { + if (m_parseGribFileWorker && !m_parseGribFileWorker->isFinished()) { m_parseGribFileWorker->abandonAndWait(); } + } + void CWeatherDataGfs::fetchWeatherData(const CWeatherGrid &grid, const CLength &range) { m_grid = grid; @@ -70,11 +74,12 @@ namespace BlackWxPlugin else { CLogMessage(this).debug() << "Using cached data"; - CWorker *worker = BlackMisc::CWorker::fromTask(this, "parseGribFile", [this]() + Q_ASSERT_X(!m_parseGribFileWorker, Q_FUNC_INFO, "Worker already running"); + m_parseGribFileWorker = BlackMisc::CWorker::fromTask(this, "parseGribFile", [this]() { parseGfsFileImpl(m_gribData); }); - worker->then(this, &CWeatherDataGfs::ps_fetchingWeatherDataFinished); + m_parseGribFileWorker->then(this, &CWeatherDataGfs::ps_fetchingWeatherDataFinished); } } @@ -92,11 +97,12 @@ namespace BlackWxPlugin void CWeatherDataGfs::ps_parseGfsFile(QNetworkReply *reply) { m_gribData = reply->readAll(); - CWorker *worker = BlackMisc::CWorker::fromTask(this, "parseGribFile", [this]() + Q_ASSERT_X(!m_parseGribFileWorker, Q_FUNC_INFO, "Worker already running"); + m_parseGribFileWorker = BlackMisc::CWorker::fromTask(this, "parseGribFile", [this]() { parseGfsFileImpl(m_gribData); }); - worker->then(this, &CWeatherDataGfs::ps_fetchingWeatherDataFinished); + m_parseGribFileWorker->then(this, &CWeatherDataGfs::ps_fetchingWeatherDataFinished); reply->deleteLater(); } @@ -199,6 +205,8 @@ namespace BlackWxPlugin g2int iseek = 0; for(;;) { + if(QThread::currentThread()->isInterruptionRequested()) { return; } + // Search next grib field g2int lskip = 0; g2int lgrib = 0; @@ -246,6 +254,8 @@ namespace BlackWxPlugin for (const GfsGridPoint &gfsGridPoint : m_gfsWeatherGrid) { + if(QThread::currentThread()->isInterruptionRequested()) { return; } + CTemperatureLayerList temperatureLayers; CAltitude surfaceAltitude (0, CAltitude::AboveGround, CLengthUnit::defaultUnit()); diff --git a/src/plugins/weatherdata/gfs/weatherdatagfs.h b/src/plugins/weatherdata/gfs/weatherdatagfs.h index 713d74308..ae522fb72 100644 --- a/src/plugins/weatherdata/gfs/weatherdatagfs.h +++ b/src/plugins/weatherdata/gfs/weatherdatagfs.h @@ -14,6 +14,7 @@ #include "g2clib/grib2.h" #include "blackmisc/weather/gridpoint.h" +#include "blackmisc/worker.h" #include "blackcore/weatherdata.h" #include #include @@ -21,6 +22,7 @@ #include #include #include +#include #include class QNetworkAccessManager; @@ -42,6 +44,9 @@ namespace BlackWxPlugin //! Constructor CWeatherDataGfs(QObject *parent = nullptr); + //! Destructor + ~CWeatherDataGfs(); + //! \copydoc BlackCore::IWeatherData::fetchWeatherData virtual void fetchWeatherData(const BlackMisc::Weather::CWeatherGrid &grid, const BlackMisc::PhysicalQuantities::CLength &range) override; @@ -161,6 +166,8 @@ namespace BlackWxPlugin QVector m_gfsWeatherGrid; BlackMisc::Weather::CWeatherGrid m_weatherGrid; + QPointer m_parseGribFileWorker; //!< worker will destroy itself, so weak pointer + using Grib2ParameterKey = std::array; using Grib2ParameterTable = QMap; static const Grib2ParameterTable m_grib2ParameterTable;