* @brief Spins up the worker thread once and arranges an orderly stop at app exit. */
| 177 | * @brief Spins up the worker thread once and arranges an orderly stop at app exit. |
| 178 | */ |
| 179 | void DataModel::JsWatchdogThread::ensureStarted() |
| 180 | { |
| 181 | if (m_started) |
| 182 | return; |
| 183 | |
| 184 | m_thread = new QThread(); |
| 185 | m_thread->setObjectName(QStringLiteral("JsWatchdog")); |
| 186 | |
| 187 | m_worker = new JsWatchdogWorker(); |
| 188 | m_worker->moveToThread(m_thread); |
| 189 | |
| 190 | QObject::connect(m_thread, &QThread::started, m_worker, &JsWatchdogWorker::begin); |
| 191 | m_thread->start(); |
| 192 | |
| 193 | if (auto* app = QCoreApplication::instance()) |
| 194 | QObject::connect(app, &QCoreApplication::aboutToQuit, app, [this]() { shutdown(); }); |
| 195 | |
| 196 | m_started = true; |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * @brief Stops the worker thread and releases it; idempotent and main-thread safe. finish() |