Reputation: 1752
I need to stop a service when our program has been killed with the task manager.
I tried with std::signal(...)
and _onexit(...)
but it does not work.
I tried running ProcessMonitor so check a sort of signal I can catch, but I did not find one.
I tried with a:
auto serviceStopThread = QThread::create([](){
::WaitForSingleObject(::GetCurrentProcess(), INFINITE);
ServiceUtils::stopService();
});
serviceStopThread->start();
but it does nothing.
How can I do?
Upvotes: 0
Views: 665
Reputation: 21936
While the process is still alive, find the PID, and open it with OpenProcess. You’ll need at least SYNCHRONIZE
permission.
Then wait for the handle to become signaled. For example, you can launch a new thread, and call WaitForSingleObject
with INFINITE
timeout. The handle becomes signaled as soon as the process quits, regardless on the reason.
React however you like but don’t forget to call CloseHandle
when you’re done.
If you only want to react when the process is killed suddenly, send some message to your supervising process when the program exits gracefully, to disable the handling.
Upvotes: 2