PDA

View Full Version : processEvents preventing class from destruction?



bowser
4th January 2008, 12:41
As I implemented my first QDSService, I thought the application would last forever after the first request for it. It was somehow true, because that single instance was staying alive for how many requests I did.
However, I made a second service, a lot simpler, and the application was created/destroyed at each service solicitation.

It turns out that the line:

QCoreApplication:: processEvents();

was preventing the application to be destroyed (i.e. no destructor called).
Since this line is needed to help synchronizing QHttp response, what exactly is wrong? The service actually responds correctly.. it just not get destroyed after that.

By the way, since I have two services that are so often used, I'd like to make them startup as soon as the mobile loads. I think, with this, it will only get instantiated once. How can I achieve that?
I tried adding the application name in tasks.cfg, but the application didn't get instantiated.

Thanks in advance.

wysota
4th January 2008, 16:14
If you process events all the time, you don't give the application a chance to actually quit the event loop because you keep triggering it by calling processEvents(). Instead you should use signals and slots with QHttp and not call processEvents().

bowser
4th January 2008, 17:21
Sorry for the lack of details, but I actually need to synchronize the qHttp usage, since it is called from a service class.
Let me try to explain in code:

1. An application calls


QDSServiceInfo service("getAllUsers", "BackendCommunication");
QDSAction* qdsAction = new QDSAction(service);
qdsAction->invoke();


2. In the BackendCommunication class, I do something like this:


http->request(header, QByteArray().append(msg));
// wait until request is done
while (responses[http] == "") {
QCoreApplication::processEvents();
}
QString response = responses[http];


That is why I used processEvents - the busy loop up there. My doubt was why it is preventing the application to quit, if the application eventually exits the method. I can successfully respond the response to the calling application.

Regarding that service lifecycle, is there something to do to avoid instancing every time? I need to have the BackendCommunication instance running as soon as possible (since it should be able to do some things without user input)

wysota
4th January 2008, 17:45
So change your code - divide it into parts and start the second part after receiving a signal from the QHttp object started in the first part. You'll be eating less cpu cycles and at the same time fix your problem.

bowser
4th January 2008, 18:05
I see... maybe putting this as a service wasn't the best idea...

But anyways, about instantiating the application right in the start, I have attempted two things:
1. adding the application in Tasks.cfg
2. adding a etc/default/Trolltech/Launcher configuration file with the application (according to
startupapplications.html (http://doc.trolltech.com/qtopia4.2/startupapplications.html))

But the application wasn't started. Can you give more details on this?