How to kill yourself in qt program?
-
@JonB
Why cannot I respond to the SERVICE_CONTROL_STOP message event when using the ControlService(scService, SERVICE_CONTROL_STOP, &status) method?SC_HANDLE scManager = OpenSCManager(nullptr, nullptr, SC_MANAGER_CREATE_SERVICE); if (!scManager) { qDebug() << "Failed to open SCManager. Error:" << GetLastError(); return false; } // std::wstring serviceNameW = serviceName.toStdWString(); SC_HANDLE scService = OpenServiceW(scManager, serviceNameW.c_str(), DELETE); if (!scService) { qDebug() << "Failed to open service. Error:" << GetLastError(); CloseServiceHandle(scManager); return false; } // SERVICE_STATUS status; ControlService(scService, SERVICE_CONTROL_STOP, &status);
@mirro
I already answered:@JonB said in How to kill yourself in qt program?:
@mirro Why not tell us whether in that case your
SERVICE_CONTROL_STOP
code is actually hit, or would you like us to guess?Do you not think we need to know whether your function is being hit? Just pasting more code and not telling us what does/does not happen is not helpful when asking a question.
Also this now a Windows programming question, nothing to do with Qt.
-
@mirro said in How to kill yourself in qt program?:
int res = QProcess::execute("kill", QStringList() << QString::number(QCoreApplication::applicationPid()));
I assume that
res
is -1 or -2 but you never check.Your code is plainly written for Windows (OpenServiceW, DeleteService, CloseServiceHandle) and there is no such beast as
kill
in that environment by default.
That call would only work on a Linux box ifkill
is in the executable's PATH at the time of this call and the SIGTERM signal is not being ignored or otherwise intercepted/blocked. If it worked then you would never expect to execute thereturn true
line.Why do you not just use one of:
-
@mirro said in How to kill yourself in qt program?:
int res = QProcess::execute("kill", QStringList() << QString::number(QCoreApplication::applicationPid()));
I assume that
res
is -1 or -2 but you never check.Your code is plainly written for Windows (OpenServiceW, DeleteService, CloseServiceHandle) and there is no such beast as
kill
in that environment by default.
That call would only work on a Linux box ifkill
is in the executable's PATH at the time of this call and the SIGTERM signal is not being ignored or otherwise intercepted/blocked. If it worked then you would never expect to execute thereturn true
line.Why do you not just use one of:
-
@ChrisW67
The QCoreApplication::quit() method is invalid, and this process program still exists.
@mirro said in How to kill yourself in qt program?:
@ChrisW67
The QCoreApplication::quit() method is invalidHave you read the documentation of the method?
and this process program still exists
That happens, when something in your code prevents the application from shutting down gracefully. E.g. a window that can’t be closed, a thread that doesn’t return. Could also be a memory leak. Definitively something in your application, that needs to be fixed.
You can directly call the
exit()
slot. That’s shoots the problem down in most cases - instead of solving it. You risk that your application crashes on shutdown, unless the problem is solved. -
@ChrisW67
The QCoreApplication::quit() method is invalid, and this process program still exists.
@mirro From your code,
uninstallService()
is called withoutapp.exec()
being called so it is not suitable to useQCoreApplication::quit()
orQCoreApplication::exit()
since there's no eventloop here.
By the way, is your program a console project? If yes it should printqDebug
messages but I don't see any output (or do you redirect them inmessageHandler
? In that case you need also post them).
This line may even never been reached. There's big chance that somewhere in your code before this line is hanging.
I think you should use debugger to check where does it stuck.
If it can reach the end ofuninstallService()
, there's no need to call anyquit()
orexit()
because it returns inmain()
. -
@ChrisW67
The QCoreApplication::quit() method is invalid, and this process program still exists.
@mirro
Your code gets the PID of itself and tries to issue an OS command ofkill <pid>
. Don't know where you got the idea of this from, Windows does not come with akill
command, so unless you have added one it won't find the command to run.I don't know why you are trying to kill yourself, but if you really want to do this you might as well call
exit()
or_exit()
directly. That is not advisable/tidy, but it's still neater than trying to issue an OS command to kill yourself.As @Bonnie has noted, if you simply allow your
uninstallService()
to run to its end execution will return intomain()
where it goesreturn 0
and so will exit anyway. We do not know why you do not allow it simply to do that.The exit, or your kill, will only happen if you invoke your program with a command line argument. The
uninstallService()
will only be hit if you invoke it asPixelStreamServerTalkTest --uninstall
. Note that has two hyphens,--uninstall
. The only way you should see your program continuing to run is if you run it with no command line arguments, i.e. plainPixelStreamServerTalkTest
. This is common for a Windows service: with command line arguments, like--install
/--uninstall
it does a corresponding action and exits, but if no arguments are passed it runs all the time doing whatever it does as a Windows service. Show how you actually invoke your program to run. -
@mirro
Your code gets the PID of itself and tries to issue an OS command ofkill <pid>
. Don't know where you got the idea of this from, Windows does not come with akill
command, so unless you have added one it won't find the command to run.I don't know why you are trying to kill yourself, but if you really want to do this you might as well call
exit()
or_exit()
directly. That is not advisable/tidy, but it's still neater than trying to issue an OS command to kill yourself.As @Bonnie has noted, if you simply allow your
uninstallService()
to run to its end execution will return intomain()
where it goesreturn 0
and so will exit anyway. We do not know why you do not allow it simply to do that.The exit, or your kill, will only happen if you invoke your program with a command line argument. The
uninstallService()
will only be hit if you invoke it asPixelStreamServerTalkTest --uninstall
. Note that has two hyphens,--uninstall
. The only way you should see your program continuing to run is if you run it with no command line arguments, i.e. plainPixelStreamServerTalkTest
. This is common for a Windows service: with command line arguments, like--install
/--uninstall
it does a corresponding action and exits, but if no arguments are passed it runs all the time doing whatever it does as a Windows service. Show how you actually invoke your program to run. -
@JonB
Why does the SERVICE_CONTROL_STOP message not respond when SetServiceStatus is set to the SERVICE_STOPPED state?serviceStatus.dwCurrentState = SERVICE_STOPPED; SetServiceStatus(serviceStatusHandle, &serviceStatus);
-
@mirro Why not tell us whether in that case your
SERVICE_CONTROL_STOP
code is actually hit, or would you like us to guess?@JonB
Why cannot I respond to the SERVICE_CONTROL_STOP message event when using the ControlService(scService, SERVICE_CONTROL_STOP, &status) method?SC_HANDLE scManager = OpenSCManager(nullptr, nullptr, SC_MANAGER_CREATE_SERVICE); if (!scManager) { qDebug() << "Failed to open SCManager. Error:" << GetLastError(); return false; } // std::wstring serviceNameW = serviceName.toStdWString(); SC_HANDLE scService = OpenServiceW(scManager, serviceNameW.c_str(), DELETE); if (!scService) { qDebug() << "Failed to open service. Error:" << GetLastError(); CloseServiceHandle(scManager); return false; } // SERVICE_STATUS status; ControlService(scService, SERVICE_CONTROL_STOP, &status);
-
@JonB
Why cannot I respond to the SERVICE_CONTROL_STOP message event when using the ControlService(scService, SERVICE_CONTROL_STOP, &status) method?SC_HANDLE scManager = OpenSCManager(nullptr, nullptr, SC_MANAGER_CREATE_SERVICE); if (!scManager) { qDebug() << "Failed to open SCManager. Error:" << GetLastError(); return false; } // std::wstring serviceNameW = serviceName.toStdWString(); SC_HANDLE scService = OpenServiceW(scManager, serviceNameW.c_str(), DELETE); if (!scService) { qDebug() << "Failed to open service. Error:" << GetLastError(); CloseServiceHandle(scManager); return false; } // SERVICE_STATUS status; ControlService(scService, SERVICE_CONTROL_STOP, &status);
@mirro
I already answered:@JonB said in How to kill yourself in qt program?:
@mirro Why not tell us whether in that case your
SERVICE_CONTROL_STOP
code is actually hit, or would you like us to guess?Do you not think we need to know whether your function is being hit? Just pasting more code and not telling us what does/does not happen is not helpful when asking a question.
Also this now a Windows programming question, nothing to do with Qt.
-