Your CService::start() function shouldn´t be blocking. It is not supposed to be the service´s main function (it took me some time to figure that out...). Using these classes there is no main function. You just get an event loop which you start in your programs main() function using myservice.exec().
To implement your functionality you have to use an event driven approach (signals and slots). Look at the following example. It writes once a second a string to your applications event log.
#ifndef SVCMAIN_H
#define SVCMAIN_H
#include <QObject>
{
Q_OBJECT
public:
explicit SvcMain
(QObject *parent
= 0);
signals:
public slots:
void handleTimerEvent();
};
#endif // SVCMAIN_H
#ifndef SVCMAIN_H
#define SVCMAIN_H
#include <QObject>
class SvcMain : public QObject
{
Q_OBJECT
public:
explicit SvcMain(QObject *parent = 0);
signals:
public slots:
void handleTimerEvent();
};
#endif // SVCMAIN_H
To copy to clipboard, switch view to plain text mode
The constructor creates a periodic timer:
#include "svcmain.h"
#include <QTimer>
#include "qtservice/qtservice.h"
SvcMain
::SvcMain(QObject *parent
) :{
connect(timer, SIGNAL(timeout()), this, SLOT(handleTimerEvent()));
timer->start(1000);
}
void SvcMain::handleTimerEvent()
{
QtServiceBase
::instance()->logMessage
(QString("handleTimerEvent"), QtServiceBase
::Information );
}
#include "svcmain.h"
#include <QTimer>
#include "qtservice/qtservice.h"
SvcMain::SvcMain(QObject *parent) :
QObject(parent)
{
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(handleTimerEvent()));
timer->start(1000);
}
void SvcMain::handleTimerEvent()
{
QtServiceBase::instance()->logMessage(QString("handleTimerEvent"), QtServiceBase::Information );
}
To copy to clipboard, switch view to plain text mode
Your start() function should look like this:
void CService::start()
{
svcmain = new SvcMain();
}
void CService::start()
{
svcmain = new SvcMain();
}
To copy to clipboard, switch view to plain text mode
svcmain is a private member variable:
private:
SvcMain* svcmain;
private:
SvcMain* svcmain;
To copy to clipboard, switch view to plain text mode
Look at the examples on how to implement the remaining functions (stop/resume/...).
Hope this helps.
Sebastian
Bookmarks