Qt Service does not enter the event loop issue
-
So, I have found out that even with
QCoreApplication *insuranceApp = application();it does not entering the event loop correctly. What's why it does not execute my code. The question is how to enter the service event loop? Thanks.I tried to run this service with the built-in
logMessagemethod:void InsuranceService::start() { logMessage("The start method executing....", QtServiceBase::Success, 0, 0); QFile *logFile = new QFile(ServiceLogger::getLogPath()); logFile->open(QIODevice::WriteOnly | QIODevice::Append); ServiceLogger::writeLog(logFile, QString("Test...").toUtf8()); logFile->close(); logFile->deleteLater(); }In the
Event Viewerit reports: The start method executing....
But for some reason everything belowlogMessage()does not execute. The log file does not exists which should be created. It could be the issue thatlogMessage()runs on one instance andQFilecreates/runs on another instance. I need somehow to connect to the right instance. -
I tried to run this service with the built-in
logMessagemethod:void InsuranceService::start() { logMessage("The start method executing....", QtServiceBase::Success, 0, 0); QFile *logFile = new QFile(ServiceLogger::getLogPath()); logFile->open(QIODevice::WriteOnly | QIODevice::Append); ServiceLogger::writeLog(logFile, QString("Test...").toUtf8()); logFile->close(); logFile->deleteLater(); }In the
Event Viewerit reports: The start method executing....
But for some reason everything belowlogMessage()does not execute. The log file does not exists which should be created. It could be the issue thatlogMessage()runs on one instance andQFilecreates/runs on another instance. I need somehow to connect to the right instance.Additionally, I created a test method in
QtServiceBaseclass.void QtServiceBase::myCustomMessage(QString path) { QFile *file = new QFile(path); file->open(QIODevice::WriteOnly); file->write("This is just a test example...."); file->close(); file->deleteLater(); }And in
InsuranceServiceclass I call mymyCustomMessage()test method.void InsuranceService::start() { logMessage("The start method executing....", QtServiceBase::Success, 0, 0); myCustomMessage("C:\\Users\\cobra\\AppData\\Local\\InsuranceService\\Log\\Test.txt"); }Now it creates the .txt file in the service! So, I was right. The issue is with the wrong instance.
Have anyone tried theQtServiceto share some details about this issue? Thanks. -
I tried to run this service with the built-in
logMessagemethod:void InsuranceService::start() { logMessage("The start method executing....", QtServiceBase::Success, 0, 0); QFile *logFile = new QFile(ServiceLogger::getLogPath()); logFile->open(QIODevice::WriteOnly | QIODevice::Append); ServiceLogger::writeLog(logFile, QString("Test...").toUtf8()); logFile->close(); logFile->deleteLater(); }In the
Event Viewerit reports: The start method executing....
But for some reason everything belowlogMessage()does not execute. The log file does not exists which should be created. It could be the issue thatlogMessage()runs on one instance andQFilecreates/runs on another instance. I need somehow to connect to the right instance.@Cobra91151 said in Qt Service does not enter the event loop issue:
But for some reason everything below logMessage() does not execute
logFile->open(QIODevice::WriteOnly | QIODevice::Append);How do you know this does not execute? The very first thing I would do is check the return result and report details of any failure.
Could you have a file permission issue? You have to be careful about the paths you pass to open and the permissions you are currently executing under.
-
@Cobra91151 said in Qt Service does not enter the event loop issue:
But for some reason everything below logMessage() does not execute
logFile->open(QIODevice::WriteOnly | QIODevice::Append);How do you know this does not execute? The very first thing I would do is check the return result and report details of any failure.
Could you have a file permission issue? You have to be careful about the paths you pass to open and the permissions you are currently executing under.
I just run
InsuranceService.exe -execincmdand it works beause I see the file is created and data is written there as well. When I runNET START InsuranceServicecommand, the service is started and running but no file is created.Also, I add my test method to
QtServiceBaseclass and call it, then the file automatically creates when I start my service. Additionally, the program has the admin privileges.I think the issue could be with the static Qt build, which could take some internal files and override them. I had some similar issues with
QFtpclass. I will try to use Qt shared build and provide all dlls components. -
I just run
InsuranceService.exe -execincmdand it works beause I see the file is created and data is written there as well. When I runNET START InsuranceServicecommand, the service is started and running but no file is created.Also, I add my test method to
QtServiceBaseclass and call it, then the file automatically creates when I start my service. Additionally, the program has the admin privileges.I think the issue could be with the static Qt build, which could take some internal files and override them. I had some similar issues with
QFtpclass. I will try to use Qt shared build and provide all dlls components.@Cobra91151 I suggested what I would do.
-
@Cobra91151 I suggested what I would do.
I already tried to log every step, anything after
myCustomMessage("C:\\Users\\cobra\\AppData\\Local\\InsuranceService\\Log\\Test.txt");will NOT execute. At this moment I do not know what is the reason for such behaviour. The app runs just fine but when it is the service it does nothing. -
I already tried to log every step, anything after
myCustomMessage("C:\\Users\\cobra\\AppData\\Local\\InsuranceService\\Log\\Test.txt");will NOT execute. At this moment I do not know what is the reason for such behaviour. The app runs just fine but when it is the service it does nothing.It only executes theQtServiceBasemethods. I can show you the example, you will see it one moment.When I hardcoded the file path to:
"C:\\Users\\cobra\\AppData\\Local\\InsuranceService\\Log\\text.txt"it successfully created the file. It seems the issue is with:QStandardPaths::writableLocation(QStandardPaths::DataLocation)but I need to debug it again. Thanks. -
I tried to run this service with the built-in
logMessagemethod:void InsuranceService::start() { logMessage("The start method executing....", QtServiceBase::Success, 0, 0); QFile *logFile = new QFile(ServiceLogger::getLogPath()); logFile->open(QIODevice::WriteOnly | QIODevice::Append); ServiceLogger::writeLog(logFile, QString("Test...").toUtf8()); logFile->close(); logFile->deleteLater(); }In the
Event Viewerit reports: The start method executing....
But for some reason everything belowlogMessage()does not execute. The log file does not exists which should be created. It could be the issue thatlogMessage()runs on one instance andQFilecreates/runs on another instance. I need somehow to connect to the right instance.@Cobra91151 said in Qt Service does not enter the event loop issue:
In the Event Viewer it reports: The start method executing....
But for some reason everything below logMessage() does not execute.logFile->open(QIODevice::WriteOnly | QIODevice::Append);Why don't you test this result? At the moment you would not know whether anything is executing after
logMessage(). That is all I am saying. -
@Cobra91151 said in Qt Service does not enter the event loop issue:
In the Event Viewer it reports: The start method executing....
But for some reason everything below logMessage() does not execute.logFile->open(QIODevice::WriteOnly | QIODevice::Append);Why don't you test this result? At the moment you would not know whether anything is executing after
logMessage(). That is all I am saying.I will debug it again because it seems some code is skipped when running it as a service. Thank you.
-
@Cobra91151 said in Qt Service does not enter the event loop issue:
In the Event Viewer it reports: The start method executing....
But for some reason everything below logMessage() does not execute.logFile->open(QIODevice::WriteOnly | QIODevice::Append);Why don't you test this result? At the moment you would not know whether anything is executing after
logMessage(). That is all I am saying.Ok. I have properly debug the app and found out that
QProcess,QStandardPaths::writableLocationandqgetenvare not working when app is a service. It seems that service functionality is limited. So, I decided to set my app to run at OS startup instead of a service. Now, it works well. The issue is resolved. Thanks. -
C Cobra91151 has marked this topic as solved on
-
C Cobra91151 has marked this topic as unsolved on
-
C Cobra91151 has marked this topic as solved on