2

In my C++ Windows Program without a console or GUI I wan't to detect if the task is being cancelled/stopped. How can I achieve this?

Thanks.

user3434681
  • 85
  • 1
  • 6

2 Answers2

5

How does your "task" run and how is it stopped? If it's just killed/terminated from the outside, then you can't detect it inside that process.

If your program exits in a regular fashion (no kill, no quick exit), you can register a "cleanup" function with atexit(), which will be run during exit().

EDIT: Or since it's C++, you could use a global variable with a custom type and run your code inside the destructor, which is basically the same as atexit().

mooware
  • 1,722
  • 2
  • 16
  • 25
3

atexit() would be the C-portable way to do it.

If you want to use a Windows specific way to do it, see this: Win32 API analog of sending/catching SIGTERM

Community
  • 1
  • 1
Tyler
  • 1,818
  • 2
  • 13
  • 22