Process is not killing if started using QProcess::startDetached()
-
I am executing one qt process using QProcess::startDetached() from my Qt application.
New process is starting successfully, but when I wanted to kill this process using QProcess::kill() OR Terminate() method my new process is not terminating.Note:- Most of the times after QProcess::startDetached() call, the pid of the newly created process is coming zero.
-
I don't get it. StartDetached is a static method. It only returns a boolean, but it does not operate on any instance of a QProcess object. It does not tell you the pid at all. How do you plan to call kill or terminate on it? These methods are slots that do operate on an instance of QProcess. What instance are you using for that?
The whole idea of using startDetached is that you do a fire-and-forget: you start the process, but you don't want any control of it anymore. It will even continue to live after your own process has terminated.
-
Thanks for your suggestion. But actually as per the documentation of startDetached() function, the last parameter returns the pid of the newly created process.
My newly launched process constructor is doing below mentioned things.
- Loading some shared library and resolving exported functions.
- Creating one localserver and connecting newconnection slot.
Sometimes I will get the pid of my newly created process in the last param of startDetached() method, but most of the times it is 0.
Please suggest how can I get the pid of newly created process.
-
You are right, the last argument of the most extended overload of startDetached allows you to get back the pId, according to the documentation. Note that that still does not change my argument above that QProcess::kill or terminate won't work on them.
Could you show some code? Judging by the documentation, if the pId returned is 0, that would mean that starting the process failed (so the return value should also be false).
-
If you are working on Windows (Vista, 7, 8), don't expect the pId you get to be any use. It might well be just an intermediate pId that Windows throws away once it starts the real process. This is done to implement the "UAC":http://en.wikipedia.org/wiki/User_Account_Control.