0

In order to prevent application's data from losing caused by "End Task" from Task Manager, I am trying to save data at the function handler of WM_CLOSE event.

The app saves data successfully in case I closed my app via Alt+F4 or "close" button. But when I killed it via the Task Manager, the saving data process couldn't be done properly. It seems that the saving progress was terminated in middle.

I tried to debug it via VS2015 IDE, the debugger intercepted a break point in the WM_CLOSE handler successfully but it could not go further, hitting F10 to step over caused my app closes immediately.

Is there any way to delay the termination progress until my application saves data completely?

I found two links below but they didn't help.

How to handle "End Task" from Windows Task Manager on a background process?

How does task manager kill my program?

Community
  • 1
  • 1
SteveH
  • 233
  • 5
  • 15
  • When you debugged your application, the task manager decided that it wasn't responding, and killed it. Have you tried writing a log file? How long does your save process take? – Dutow Jul 22 '16 at 09:20
  • @Dutwo It usually takes less than 500ms. I tried log to a file but sometimes it logged, not always. – SteveH Jul 22 '16 at 09:29
  • If I want to kill your application in the task manager, then why would I want it to save anything?! All I want is the process to terminate immediately. Be user-friendly. – Christian Hackl Jul 22 '16 at 09:43
  • @ Christian Hackl, Right! I have strongly argued my customers about this matter but they insisted to have this "feature". – SteveH Jul 22 '16 at 09:56

1 Answers1

2

The task manager might decide that your application isn't responding, and terminate it. You can do nothing against it.

If you want to ensure that your data is always saved, you should save constantly (with some heuristics, like at least once every minute, preferrably after no change happened in a few seconds) in the background. It's more complex but has the advantage of working even when you won't receive WM_CLOSE at all, for example in the case of power loss.

Dutow
  • 5,638
  • 1
  • 30
  • 40
  • I am sure that WM_CLOSE event always gets intercepted when my app killed by the Task Manager. The matter as I mentioned above is the termination occurs too quickly so that the saving operation can't be complete. I can do saving in a certain period but the matter is data changes often, too many unnecessary saving should affect the performance. – SteveH Jul 22 '16 at 09:59
  • Yes, but you can't do anything against termination, you won't get a notification about it and can't delay it. If the system decides to terminate your app, it will terminate it immediately. – Dutow Jul 22 '16 at 10:00