Starting and terminating processes needed by my application
Hi! I'm working on an application which needs two other processes to work. The processes must be started and must be terminated when my application starts and quits. These processes may take some time to start, so I would also like to load in a detached thread to prevent my interface from appearing after some seconds:
@mythread = new QThread();
QObject::connect(&a, SIGNAL(aboutToQuit()), ProcessHolder::getInstance(), SLOT(mydestroy()));
QObject::connect(&a, SIGNAL(aboutToQuit()), mythread, SLOT(quit()));
QObject::connect(mythread, SIGNAL(finished()), mythread, SLOT(deleteLater()));
the mydestroy() method terminates the processes and deleteLater() the object.
This works correctly when I start my application, wait for the processes to start successfully, then quit my application. The processes are correctly terminated.
If I start my application and immediately close it, before the processes are completely started, my mydestroy() method is not even invoked. Maybe because the thread owning the object is still busy when mydestroy() is invoked?
Any idea what I can do to fix this?
What about catching the quit signal in a specific method? This way you could delay main app's quit process long enough for the other processes to finish loading.
(like, using sigaction, SIGQUIT?, sleep...)
So I should put something like a sleep in the signal handler to give needed time to the other threads to finish the cleanup process?
There is no need at all to use threads to start external processes. QProcess will suffice, and is asynchronic by itself already. Simply don't use the synchronous (waitFor...) API, and you have an async setup.
I know that. But I also do other initializations, which are not asynchronous, so that solution seems pretty good. And the initialization doesn't suffer issues of any kind at the moment. My problem is related to the cleanup. Why isn't that method invoked? I need to invoke that method to terminate correctly my processes and cleanup. This way instead, my processes remain running.
At the end, I came up with a solution which seems to work for the moment. The problem I suppose was related to the fact that the application was terminated before the mydestroy() method ended.
This executes the method in the same thread and seems to work better:
@QObject::connect(&a, SIGNAL(aboutToQuit()), ProcessHolder::getInstance(), SLOT(mydestroy()), Qt:DirectConnection);@