My application starts a process and I need it to automatically close when the application closes. The application can be closed by the Task Manager, Ctrl + C or some other standard close operation.
I've tried using:
signal(SIGTERM, callback)
fromsignal.h
SetConsoleCtrlHandler(callback, TRUE)
fromwindows.h
atexit(callback)
fromstdlib.h
But none of them did the job.
Using signal
#include <iostream>
#include <csignal>
void onExit(int sig) {
std::cout << "sig(" << sig << ")" << std::endl;
}
int main() {
signal(SIGINT, onExit);
int n;
std::cin >> n;
}
Using SetConsoleCtrlHandler
I've copied the code from MSDN
Using atexit
#include <stdlib.h>
void onExit(void) {
std::cout << "sig(" << ")" << std::endl;
}
int main() {
atexit(onExit);
int n;
std::cin >> n;
}
None of the above solution called the callback when I terminated the application from the Task Manager nor the close button.
What should I do?