3

I'm trying to develop a C++ MFC application which runs in background (in Windows) and I have one question that I can't find a complete response.

How do I to detect when the user try to close it via Task Manager by killing process, when it would be closed by shutting down the computer and when the computer in which this app runs crashs ?

Knowing that I have an external Database (SQL Server 2008) with which this app interacts.

3
  • When computer is in process of shutting down and user won't forcing it I guess just WM_QUIT message is being sent so you may send something to your database in this case but it's better to close app soon after that. In other cases I guess there's nothing you can do except detecting that your application suddenly stop giving responses.
    – Predelnik
    Feb 8, 2014 at 12:44
  • With regard to your first question, there are two ways to shut down a process via the task manager. One is detectable by the process, the other isn't. If the latter was used your application is toast. That's by design. You can have separate watchdog applications, but who watches the watchdogs? With regard to your last question, when the computer crashes it is malfunctioning in the worst way. You are asking how to unburn burnt toast. You can't. Feb 8, 2014 at 13:02
  • 1
    @DavidHammen, I have an idea but I'm not sure, I think that the app will ping SQL Server each 30minutes and that I'll develop another module which runs in that server which listens each ping, and if there is no ping after 30minutes it'll just add a new line in the database, what do you think about? Feb 8, 2014 at 13:17

1 Answer 1

1

You may handle WM_QUIT as suggested above. There is also WM_QUERYENDSESSION and its family.

For service that without GUI, use RegisterServiceCtrlHandlerEx to get notified.

On the other hand, to intercept when your own application crash, you mess with the structured exception handlers, although when it happen you may not have reliable data.

Last, there is nothing you can do when the computer itself crash, the behavior is simply undefined.

Anyway, you cannot ensure there is a healthy DB session, there are infinite reason you may lost the DB session even like idle too long.

You should design the system to have some fault tolerance for extreme corner cases, like using transactions, rollback mechanism, traceable records when it hit the fan, etc.

0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.