7

Guys sorry if I have offended someone by asking such a noob question, as I see someone have "Marked Down" Question for being inappropriate.

This is my first time asking here so forgive me if something isn't appropriate, and sorry if my English isn't very good.

I'm trying to create a temp file when my program had been closed via the task manager ... but i have no idea about how to detect if my program is closed via the task manager !!!

How do I do that? I googled a lot, but either I used the wrong keywords or there are no simple solutions on the internet. I hope somebody here can help me.

Best regards and thanks in advance.

Ahmad MOUSSA
  • 2,729
  • 19
  • 31
  • Need more information. Operating system? Do you want the Java program to create the file or something else? Why do you want the file? What are you trying to accomplish? – RobertB Nov 18 '13 at 18:19
  • Yes dear .. i want the java program to create the tmp file befor it colsed via the task manager .... i know how to create a tmp file but i dont know how to catch if the closing is via the task manager – Ahmad MOUSSA Nov 18 '13 at 18:24
  • May be you can run other java application (using Runtime.getRuntime().exec() ) that will do tmp file creation. In this case you can check process is running or not. – Vitalii Pro Nov 18 '13 at 18:34
  • dear ... the file wich i want to create it is related in the data in the program wich forced to close by the task manager !!! – Ahmad MOUSSA Nov 18 '13 at 18:42

1 Answers1

6

To catch regular terminate requests like SIGTERM or WM_CLOSE, as sent via the taskmanager's 'Applications' tab, a shutdown hook can be installed (I assume, you're using Windows, but that doesn't make much of a difference).

However, when killing from the 'processes' tab, or with taskkill /f ... on Windows or kill -9 ... on Linux or Unix, the process (VM) gets forcibly terminated, unable react. The same goes for kills done via 'application not responding' dialogs on Windows.

So you need to make sure, that you can handle files, which are corrupted/incomplete as a result of a forced kill (apart from power outages or hardware failures).

Community
  • 1
  • 1
Sam
  • 7,778
  • 1
  • 23
  • 49
  • Dear @Sam ... you are so right ... I know all what you have said ... but i have no idea how to catch this killing in my java program !!! – Ahmad MOUSSA Nov 18 '13 at 18:31
  • 1
    No, you can't at this point. Even though different languages provide access to different signal and exit handling mechanisms with certain differences, the outcome is the same (in user space, of course). Even if a process is locked in an uninterruptible system call for a while, it gets terminated, as soon as it is finished. Thy only option is to maintain consistency during regular execution, by using transactional IO, paid with performance losses. An example would be file based database systems like sqlite, which offer the option to additionally flush the disk cache on commit (sync). – Sam Nov 18 '13 at 18:44
  • Big thanks for you @Sam .... it helped a lot .... i will mark your answer as true (y) – Ahmad MOUSSA Nov 18 '13 at 18:48