c++ Qt Service未进入事件循环问题

fivyi3re  于 2023-06-25  发布在  其他
关注(0)|答案(1)|浏览(322)

我使用了旧的QtService代码:https://github.com/qtproject/qt-solutions/tree/master/qtservice并创建了一个服务来运行npm start命令,该命令将在Windows启动时启动我的网站。当添加-exec参数时,在app模式下启动它运行得很好,但当它作为服务运行时,它什么也不做。在调试过程中,我发现它没有正确地进入服务事件循环。因此,我创建了一个小示例,它应该将一些文本写入txt文件来说明这个问题。

代码:

  • 保险服务.h*
  1. #ifndef INSURANCESERVICE_H
  2. #define INSURANCESERVICE_H
  3. #include <QCoreApplication>
  4. #include <QObject>
  5. #include <QThread>
  6. #include <QDebug>
  7. #include "qtservice.h"
  8. #include "servicelogger.h"
  9. class InsuranceService : public QtService<QCoreApplication>
  10. {
  11. Q_GADGET
  12. public:
  13. InsuranceService(int argc, char *argv[]);
  14. ~InsuranceService();
  15. private:
  16. void start();
  17. void stop();
  18. void pause();
  19. void resume();
  20. void processCommand(int code);
  21. void createApplication(int &argc, char *argv[]);
  22. };
  23. #endif // INSURANCESERVICE_H
  • 保险服务.cpp*
  1. #include "insuranceservice.h"
  2. InsuranceService::InsuranceService(int argc, char *argv[]) : QtService<QCoreApplication>(argc, argv, "InsuranceService")
  3. {
  4. std::cout << serviceName().toStdString().c_str() << std::endl;
  5. setServiceDescription("Test service");
  6. setServiceFlags(QtServiceBase::CanBeSuspended);
  7. //setStartupType(QtServiceController::AutoStartup);
  8. }
  9. void InsuranceService::start()
  10. {
  11. try {
  12. QCoreApplication *insuranceApp = application();
  13. QFile *logFile = new QFile(ServiceLogger::getLogPath(), insuranceApp);
  14. logFile->open(QIODevice::WriteOnly | QIODevice::Append);
  15. ServiceLogger::writeLog(logFile, QString("Test...").toUtf8());
  16. logFile->close();
  17. logFile->deleteLater();
  18. } catch (...) {
  19. qCritical() << "Failed to start the service!!!";
  20. }
  21. }
  22. void InsuranceService::stop()
  23. {
  24. }
  25. void InsuranceService::pause()
  26. {
  27. }
  28. void InsuranceService::resume()
  29. {
  30. }
  31. void InsuranceService::processCommand(int code)
  32. {
  33. Q_UNUSED(code);
  34. }
  35. void InsuranceService::createApplication(int &argc, char *argv[])
  36. {
  37. QtService::createApplication(argc, argv);
  38. }
  39. InsuranceService::~InsuranceService()
  40. {
  41. }

main.cpp

  1. #include <QCoreApplication>
  2. #include "insuranceservice.h"
  3. int main(int argc, char *argv[])
  4. {
  5. InsuranceService service(argc, argv);
  6. return service.exec();
  7. }

如何正确进入服务事件循环?

更新时间:

我尝试使用内置的logMessage方法运行此服务:

  1. void InsuranceService::start()
  2. {
  3. logMessage("The start method executing....", QtServiceBase::Success, 0, 0);
  4. QFile *logFile = new QFile(ServiceLogger::getLogPath());
  5. logFile->open(QIODevice::WriteOnly | QIODevice::Append);
  6. ServiceLogger::writeLog(logFile, QString("Test...").toUtf8());
  7. logFile->close();
  8. logFile->deleteLater();
  9. }

Event Viewer中,它报告:* start方法正在执行....*
但由于某些原因,logMessage()以下的所有内容都不会执行。应该创建的日志文件不存在。问题可能是logMessage()在一个示例上运行,而QFile在另一个示例上创建/运行。我需要以某种方式连接到正确的示例。
谢谢你。

gab6jxml

gab6jxml1#

好的。我已经正确地调试了应用程序,发现当应用程序是服务时,QProcessQStandardPaths::writableLocationqgetenv不工作。服务功能似乎有限。因此,我决定将我的应用程序设置为在操作系统启动时运行,而不是服务。现在,它运行良好。问题已解决。谢谢

相关问题