From 192591c5a1e8bbf01813d9ae136341d1d39b3427 Mon Sep 17 00:00:00 2001 From: Cuong Trinh Date: Tue, 15 Dec 2020 11:04:50 +0700 Subject: [PATCH] add atomic variable to check application ready. --- .../windows/windowsservicebackend.cpp | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/plugins/servicebackends/windows/windowsservicebackend.cpp b/src/plugins/servicebackends/windows/windowsservicebackend.cpp index b37a2eb..8c5b73a 100644 --- a/src/plugins/servicebackends/windows/windowsservicebackend.cpp +++ b/src/plugins/servicebackends/windows/windowsservicebackend.cpp @@ -52,6 +52,7 @@ Q_LOGGING_CATEGORY(logBackend, "qt.service.plugin.windows.backend") QPointer WindowsServiceBackend::_backendInstance; static QString xPath; +static std::atomic isApplicationReady{false}; WindowsServiceBackend::WindowsServiceBackend(Service *service) : ServiceBackend{service} @@ -130,6 +131,7 @@ int WindowsServiceBackend::runService(int &argc, char **argv, int flags) //execute the app qCDebug(logBackend) << "running application"; + isApplicationReady = true; _status.dwServiceSpecificExitCode = app.exec(); if(_status.dwServiceSpecificExitCode != EXIT_SUCCESS) _status.dwWin32ExitCode = ERROR_SERVICE_SPECIFIC_ERROR; @@ -230,10 +232,14 @@ void WindowsServiceBackend::serviceMain(DWORD dwArgc, wchar_t **lpszArgv) lock.unlock(); // wait for the mainthread to finish startup, then register the service handler - lock.relock(); - qCDebug(logBackend) << "wait for main thread to finish startup"; - _backendInstance->_startCondition.wait(&_backendInstance->_svcLock); - lock.unlock(); + // we wait until variable isApplicationReady become true + while (!isApplicationReady) + { + lock.relock(); + qCDebug(logBackend) << "wait for main thread to finish startup"; + _backendInstance->_startCondition.wait(&_backendInstance->_svcLock, 5000); + lock.unlock(); + } // handle the start event qCDebug(logBackend) << "handle service start event";