<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Qt Service does not enter the event loop issue]]></title><description><![CDATA[<p dir="auto">I used the old QtService code from: <a href="https://github.com/qtproject/qt-solutions/tree/master/qtservice" target="_blank" rel="noopener noreferrer nofollow ugc">https://github.com/qtproject/qt-solutions/tree/master/qtservice</a> and created a service to run <code>npm start</code> command which will start my website on Windows startup. When adding <code>-exec</code> argument, starting in app mode it works well but when it runs as a service it does nothing. During a debugging I found out that it does not entering the service event loop correctly. So, I created a small example which should write some text to a txt file to illustrate this issue.</p>
<p dir="auto"><strong>Code:</strong></p>
<p dir="auto"><em>insuranceservice.h</em></p>
<pre><code>#ifndef INSURANCESERVICE_H
#define INSURANCESERVICE_H

#include &lt;QCoreApplication&gt;
#include &lt;QObject&gt;
#include &lt;QThread&gt;
#include &lt;QDebug&gt;
#include "qtservice.h"
#include "servicelogger.h"

class InsuranceService : public QtService&lt;QCoreApplication&gt;
{
    Q_GADGET
public:
    InsuranceService(int argc, char *argv[]);
    ~InsuranceService();

private:
    void start();
    void stop();
    void pause();
    void resume();
    void processCommand(int code);
    void createApplication(int &amp;argc, char *argv[]);
};

#endif // INSURANCESERVICE_H
</code></pre>
<p dir="auto"><em>insuranceservice.cpp</em></p>
<pre><code>#include "insuranceservice.h"

InsuranceService::InsuranceService(int argc, char *argv[]) : QtService&lt;QCoreApplication&gt;(argc, argv, "InsuranceService")
{
    std::cout &lt;&lt; serviceName().toStdString().c_str() &lt;&lt; std::endl;
    setServiceDescription("Test service");
    setServiceFlags(QtServiceBase::CanBeSuspended);
    //setStartupType(QtServiceController::AutoStartup);
}

void InsuranceService::start()
{
    try {
        QCoreApplication *insuranceApp = application();
        QFile *logFile = new QFile(ServiceLogger::getLogPath(), insuranceApp); 
        logFile-&gt;open(QIODevice::WriteOnly | QIODevice::Append);
        ServiceLogger::writeLog(logFile, QString("Test...").toUtf8());
        logFile-&gt;close();
        logFile-&gt;deleteLater();
    } catch (...) {
        qCritical() &lt;&lt; "Failed to start the service!!!";
    }
}

void InsuranceService::stop()
{

}

void InsuranceService::pause()
{

}

void InsuranceService::resume()
{

}

void InsuranceService::processCommand(int code)
{
    Q_UNUSED(code);
}

void InsuranceService::createApplication(int &amp;argc, char *argv[])
{
    QtService::createApplication(argc, argv);
}

InsuranceService::~InsuranceService()
{

}
</code></pre>
<p dir="auto"><strong>main.cpp</strong></p>
<pre><code>#include &lt;QCoreApplication&gt;
#include "insuranceservice.h"

int main(int argc, char *argv[])
{
    InsuranceService service(argc, argv);
    return service.exec();
}
</code></pre>
<p dir="auto">Any ideas how to enter the service event loop correctly? Thank you.</p>
]]></description><link>https://forum.qt.io/topic/145857/qt-service-does-not-enter-the-event-loop-issue</link><generator>RSS for Node</generator><lastBuildDate>Thu, 05 Nov 2026 06:35:08 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/145857.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 18 Jun 2023 22:28:12 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Qt Service does not enter the event loop issue on Mon, 19 Jun 2023 20:04:01 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> <a class="plugin-mentions-user plugin-mentions-a" href="/user/chrisw67">@<bdi>ChrisW67</bdi></a></p>
<p dir="auto">Ok. I have properly debug the app and found out that <code>QProcess</code>, <code>QStandardPaths::writableLocation</code> and <code>qgetenv</code> are 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.</p>
]]></description><link>https://forum.qt.io/post/761982</link><guid isPermaLink="true">https://forum.qt.io/post/761982</guid><dc:creator><![CDATA[Cobra91151]]></dc:creator><pubDate>Mon, 19 Jun 2023 20:04:01 GMT</pubDate></item><item><title><![CDATA[Reply to Qt Service does not enter the event loop issue on Mon, 19 Jun 2023 17:36:17 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a></p>
<p dir="auto">I will debug it again because it seems some code is skipped when running it as a service. Thank you.</p>
]]></description><link>https://forum.qt.io/post/761955</link><guid isPermaLink="true">https://forum.qt.io/post/761955</guid><dc:creator><![CDATA[Cobra91151]]></dc:creator><pubDate>Mon, 19 Jun 2023 17:36:17 GMT</pubDate></item><item><title><![CDATA[Reply to Qt Service does not enter the event loop issue on Mon, 19 Jun 2023 17:33:14 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/cobra91151">@<bdi>Cobra91151</bdi></a> said in <a href="/post/761927">Qt Service does not enter the event loop issue</a>:</p>
<blockquote>
<p dir="auto">In the Event Viewer it reports: The start method executing....<br />
But for some reason everything below logMessage() does not execute.</p>
</blockquote>
<blockquote>
<p dir="auto"><code>logFile-&gt;open(QIODevice::WriteOnly | QIODevice::Append);</code></p>
</blockquote>
<p dir="auto">Why don't you test this result?  At the moment you would not know whether anything is executing after <code>logMessage()</code>.  That is all I am saying.</p>
]]></description><link>https://forum.qt.io/post/761953</link><guid isPermaLink="true">https://forum.qt.io/post/761953</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Mon, 19 Jun 2023 17:33:14 GMT</pubDate></item><item><title><![CDATA[Reply to Qt Service does not enter the event loop issue on Mon, 19 Jun 2023 17:34:45 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a></p>
<p dir="auto"><s>It only executes the <code>QtServiceBase</code> methods. I can show you the example, you will see it one moment.</s></p>
<p dir="auto">When I hardcoded the file path to: <code>"C:\\Users\\cobra\\AppData\\Local\\InsuranceService\\Log\\text.txt"</code> it successfully created the file. It seems the issue is with: <code>QStandardPaths::writableLocation(QStandardPaths::DataLocation)</code> but I need to debug it again. Thanks.</p>
]]></description><link>https://forum.qt.io/post/761950</link><guid isPermaLink="true">https://forum.qt.io/post/761950</guid><dc:creator><![CDATA[Cobra91151]]></dc:creator><pubDate>Mon, 19 Jun 2023 17:34:45 GMT</pubDate></item><item><title><![CDATA[Reply to Qt Service does not enter the event loop issue on Mon, 19 Jun 2023 17:11:54 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a></p>
<p dir="auto">I already tried to log every step, anything after <code>myCustomMessage("C:\\Users\\cobra\\AppData\\Local\\InsuranceService\\Log\\Test.txt");</code> 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.</p>
]]></description><link>https://forum.qt.io/post/761948</link><guid isPermaLink="true">https://forum.qt.io/post/761948</guid><dc:creator><![CDATA[Cobra91151]]></dc:creator><pubDate>Mon, 19 Jun 2023 17:11:54 GMT</pubDate></item><item><title><![CDATA[Reply to Qt Service does not enter the event loop issue on Mon, 19 Jun 2023 17:07:38 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/cobra91151">@<bdi>Cobra91151</bdi></a> I suggested what I would do.</p>
]]></description><link>https://forum.qt.io/post/761947</link><guid isPermaLink="true">https://forum.qt.io/post/761947</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Mon, 19 Jun 2023 17:07:38 GMT</pubDate></item><item><title><![CDATA[Reply to Qt Service does not enter the event loop issue on Mon, 19 Jun 2023 17:09:03 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a></p>
<p dir="auto">I just run <code>InsuranceService.exe -exec</code> in <code>cmd</code> and it works beause I see the file is created and data is written there as well. When I run <code>NET START InsuranceService</code> command, the service is started and running but no file is created.</p>
<p dir="auto">Also, I add my test method to <code>QtServiceBase</code> class and call it, then the file automatically creates when I start my service. Additionally, the program has the admin privileges.</p>
<p dir="auto">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 <code>QFtp</code> class. I will try to use Qt shared build and provide all dlls components.</p>
]]></description><link>https://forum.qt.io/post/761946</link><guid isPermaLink="true">https://forum.qt.io/post/761946</guid><dc:creator><![CDATA[Cobra91151]]></dc:creator><pubDate>Mon, 19 Jun 2023 17:09:03 GMT</pubDate></item><item><title><![CDATA[Reply to Qt Service does not enter the event loop issue on Mon, 19 Jun 2023 17:01:12 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/cobra91151">@<bdi>Cobra91151</bdi></a> said in <a href="/post/761927">Qt Service does not enter the event loop issue</a>:</p>
<blockquote>
<p dir="auto">But for some reason everything below logMessage() does not execute</p>
</blockquote>
<pre><code>logFile-&gt;open(QIODevice::WriteOnly | QIODevice::Append);
</code></pre>
<p dir="auto">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.</p>
<p dir="auto">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.</p>
]]></description><link>https://forum.qt.io/post/761944</link><guid isPermaLink="true">https://forum.qt.io/post/761944</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Mon, 19 Jun 2023 17:01:12 GMT</pubDate></item><item><title><![CDATA[Reply to Qt Service does not enter the event loop issue on Mon, 19 Jun 2023 16:39:36 GMT]]></title><description><![CDATA[<p dir="auto">Additionally, I created a test method in <code>QtServiceBase</code> class.</p>
<pre><code>void QtServiceBase::myCustomMessage(QString path)
{
    QFile *file = new QFile(path);
    file-&gt;open(QIODevice::WriteOnly);
    file-&gt;write("This is just a test example....");
    file-&gt;close();
    file-&gt;deleteLater();
}
</code></pre>
<p dir="auto">And in <code>InsuranceService</code> class I call my <code>myCustomMessage()</code> test method.</p>
<pre><code>void InsuranceService::start()
{
    logMessage("The start method executing....", QtServiceBase::Success, 0, 0);
    myCustomMessage("C:\\Users\\cobra\\AppData\\Local\\InsuranceService\\Log\\Test.txt");
}
</code></pre>
<p dir="auto">Now it creates the .txt file in the service! So, I was right. The issue is with the wrong instance.<br />
Have anyone tried the <code>QtService</code> to share some details about this issue? Thanks.</p>
]]></description><link>https://forum.qt.io/post/761939</link><guid isPermaLink="true">https://forum.qt.io/post/761939</guid><dc:creator><![CDATA[Cobra91151]]></dc:creator><pubDate>Mon, 19 Jun 2023 16:39:36 GMT</pubDate></item><item><title><![CDATA[Reply to Qt Service does not enter the event loop issue on Mon, 19 Jun 2023 15:06:43 GMT]]></title><description><![CDATA[<p dir="auto">I tried to run this service with the built-in <code>logMessage</code> method:</p>
<pre><code>void InsuranceService::start()
{
    logMessage("The start method executing....", QtServiceBase::Success, 0, 0);
    QFile *logFile = new QFile(ServiceLogger::getLogPath());
    logFile-&gt;open(QIODevice::WriteOnly | QIODevice::Append);
    ServiceLogger::writeLog(logFile, QString("Test...").toUtf8());
    logFile-&gt;close();
    logFile-&gt;deleteLater();
}
</code></pre>
<p dir="auto">In the <code>Event Viewer</code> it reports: <em>The start method executing....</em><br />
But for some reason everything below <code>logMessage()</code> does not execute. The log file does not exists which should be created. It could be the issue that <code>logMessage()</code> runs on one instance and <code>QFile</code> creates/runs on another instance. I need somehow to connect to the right instance.</p>
]]></description><link>https://forum.qt.io/post/761927</link><guid isPermaLink="true">https://forum.qt.io/post/761927</guid><dc:creator><![CDATA[Cobra91151]]></dc:creator><pubDate>Mon, 19 Jun 2023 15:06:43 GMT</pubDate></item><item><title><![CDATA[Reply to Qt Service does not enter the event loop issue on Mon, 19 Jun 2023 12:15:28 GMT]]></title><description><![CDATA[<p dir="auto">So, I have found out that even with <code>QCoreApplication *insuranceApp = application();</code> 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.</p>
]]></description><link>https://forum.qt.io/post/761907</link><guid isPermaLink="true">https://forum.qt.io/post/761907</guid><dc:creator><![CDATA[Cobra91151]]></dc:creator><pubDate>Mon, 19 Jun 2023 12:15:28 GMT</pubDate></item><item><title><![CDATA[Reply to Qt Service does not enter the event loop issue on Mon, 19 Jun 2023 11:46:19 GMT]]></title><description><![CDATA[<p dir="auto">I updated the main post.</p>
<p dir="auto">Additionally, I will create the logger to check what exectly executes in the start method.</p>
]]></description><link>https://forum.qt.io/post/761903</link><guid isPermaLink="true">https://forum.qt.io/post/761903</guid><dc:creator><![CDATA[Cobra91151]]></dc:creator><pubDate>Mon, 19 Jun 2023 11:46:19 GMT</pubDate></item><item><title><![CDATA[Reply to Qt Service does not enter the event loop issue on Sun, 18 Jun 2023 23:11:44 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/chrisw67">@<bdi>ChrisW67</bdi></a></p>
<p dir="auto">I already tried <code>node-windows</code> but it fails for me. That's why I decided to write my own service.</p>
<p dir="auto">I think, I know what is wrong. I found a similar issue: <a href="https://stackoverflow.com/questions/10027306/windows-service-doesnt-execute-onstart" target="_blank" rel="noopener noreferrer nofollow ugc">https://stackoverflow.com/questions/10027306/windows-service-doesnt-execute-onstart</a></p>
<p dir="auto"><em>All you should really do in the start event is fire up a worker thread that will run as long as your service is active and do the actual processing. You can use a BackgroundWorker component to simplify this. When the service is stopped you do any necessary cleanup, and terminate the background thread.</em></p>
<p dir="auto">I should create the worker class and a thread and do all processing there. <code>start()</code> method should call Worker class with <code>QThread</code>, <code>stop()</code> method should stop the thread and clear all resources. I think, the app event loop goes out of scope or something else occurs when in a service mode. Thanks.</p>
]]></description><link>https://forum.qt.io/post/761836</link><guid isPermaLink="true">https://forum.qt.io/post/761836</guid><dc:creator><![CDATA[Cobra91151]]></dc:creator><pubDate>Sun, 18 Jun 2023 23:11:44 GMT</pubDate></item><item><title><![CDATA[Reply to Qt Service does not enter the event loop issue on Sun, 18 Jun 2023 23:04:22 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/cobra91151">@<bdi>Cobra91151</bdi></a> I am not a real Node.js person.  Does <a href="https://github.com/coreybutler/node-windows" target="_blank" rel="noopener noreferrer nofollow ugc">this help</a>?</p>
]]></description><link>https://forum.qt.io/post/761835</link><guid isPermaLink="true">https://forum.qt.io/post/761835</guid><dc:creator><![CDATA[ChrisW67]]></dc:creator><pubDate>Sun, 18 Jun 2023 23:04:22 GMT</pubDate></item><item><title><![CDATA[Reply to Qt Service does not enter the event loop issue on Sun, 18 Jun 2023 23:01:27 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/chrisw67">@<bdi>ChrisW67</bdi></a></p>
<p dir="auto">Yes, you are right. So, how to provide to this service the access to all these things: environment variables (PATH etc), console IO, etc?</p>
<p dir="auto">There are a lot of other services which do something, for example Google Update service etc.</p>
<p dir="auto">Thanks.</p>
]]></description><link>https://forum.qt.io/post/761834</link><guid isPermaLink="true">https://forum.qt.io/post/761834</guid><dc:creator><![CDATA[Cobra91151]]></dc:creator><pubDate>Sun, 18 Jun 2023 23:01:27 GMT</pubDate></item><item><title><![CDATA[Reply to Qt Service does not enter the event loop issue on Sun, 18 Jun 2023 22:47:34 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/cobra91151">@<bdi>Cobra91151</bdi></a> The service will, by default, be running as a system account with no access to things like network shares, environment variables (PATH etc), console IO, etc.  When you run it from a command prompt it has these things.</p>
<p dir="auto">What can be going wrong is very dependent on exactly what your <code>npm</code> and app instance needs.</p>
]]></description><link>https://forum.qt.io/post/761832</link><guid isPermaLink="true">https://forum.qt.io/post/761832</guid><dc:creator><![CDATA[ChrisW67]]></dc:creator><pubDate>Sun, 18 Jun 2023 22:47:34 GMT</pubDate></item></channel></rss>