1

I just need to detect when my WPF application is killed from "Task Manager" --> "Process" tab, not from "Application" tab.

again mentioning just need to detect not stop or intercepting!

a piece of C# code would do fine.

Ayazz
  • 53
  • 7
  • i have seen many threads regarding process and killing applications. there is no solid evidence is it possible or even not ! – Ayazz Mar 12 '16 at 16:52
  • You need an extra process right, because you can't do work in a process that has been terminated – David Heffernan Mar 12 '16 at 17:15

1 Answers1

1

As @DavidHeffernan already told you you have to use another process/service that will check for your process termination using one of the techniques from How to detect win32 process creation/termination in c++.

You will just have to properly reinitialize this process/service while starting your actual application.

But how to understand that your process has been terminated forcefully and has not been peacefully closed?

  1. You may try to hook on TerminateProcess - it is not the easiest way (probably one of the hardest, actually), and it will not handle NtTerminateProcess by itself, so... - that won't work, unfortunately - Why can’t you trap TerminateProcess? :

TerminateProcess is the low-level process killing function. It bypasses DLL_PROCESS_DETACH and anything else in the process. Once you kill with TerminateProcess, no more user-mode code will run in that process. It’s gone. Do not pass go. Do not collect $200.

  1. You'd be much better with some synchronization object - in the simplest way it can just be an indicator directory your application deletes when it is properly closed (manually or through external WM_CLOSE message). Then the service checks whether that directory exists or not (some race condition is actually possible if your application is restarted before service checks the indicator directory)
  2. As a more advanced variant of 2. you can use a named mutex(or perhaps better a named event as pointed @IInspectable) created by your actual application and wait for either that mutex or process termination with WaitForMultipleObject. It is a more robust solution than 2, but there may be some implementation details to consider.

P.S.: In any case you will have to carefully consider the management of the service that will watch for your application. Race conditions and other nasty things can occur. Especially if your application can be launched in multiple instances.

Community
  • 1
  • 1
Eugene Podskal
  • 10,270
  • 5
  • 31
  • 53
  • For option 3. a named event object (see [CreateEvent](https://msdn.microsoft.com/en-us/library/windows/desktop/ms682396.aspx)) is more appropriate. That way you don't have to worry about `WAIT_ABANDONED_0` returned by `WaitForMultipleObjects`. – IInspectable Mar 12 '16 at 17:47
  • @IInspectable Thanks for pointing it. I didn't thought about all the details, just how it will look overall in broad strokes. – Eugene Podskal Mar 12 '16 at 17:51
  • I really understand all the point mentioned by @Eugene podskal .. I am bound to not use any service strictly or either hook up terminateprocess with my main process as I tried in my case but couldn't got notified in my metro UI implemented App application. – Ayazz Mar 13 '16 at 19:17
  • But in windows 7 as far as I learned .. there is limitation aswell to hookup terminateprocess.. may be I'm wrong.. still I'm unable to catch in c#wpf application.. not to for metro UI by mahapps – Ayazz Mar 13 '16 at 19:19
  • Can anyone 100 percent claim it is possible or not without using service? Wpf metro UI app with environment of windows 7 or windows 8?? – Ayazz Mar 13 '16 at 19:22
  • @ayaz Hooking terminateProcess is actually the last thing I'd probably tried (even with proper libraries it can be difficult to do right). And by service I mean not exactly service(though it could be it), but any other application. Metro UI issues? As far as I know there is still a separate process for each application, so everything should be the same as with simple applications, though I may be wrong and the applications are somehow hosted in a different way. – Eugene Podskal Mar 13 '16 at 19:32
  • yeah @EugenePodskal i got through from service and then after being rejected , i tried creating dependency object eventually got through aswell.. but management simply dont want to depend on other service/application strictly.. i am unable to find a way to simple get notified in my app .. believe my i tried every possible method available including terminateprocess :) . i can share those if you want to know – Ayazz Mar 13 '16 at 19:52
  • @ayaz If you have some problems with terminateprocess hook, then you should post a separate question - someone with more hands on experience may help. Also you may try to "placate/cheat" your management with its [Baron Munchausen](https://en.wikipedia.org/wiki/Baron_Munchausen)-like [requirements](https://upload.wikimedia.org/wikipedia/commons/3/3b/Muenchhausen_Herrfurth_7_500x789.jpg) by embedding the second application inside the main application as a resource. – Eugene Podskal Mar 13 '16 at 21:10
  • @EugenePodskal that would be very bad idea if i cheated on management, as it would someday eventually got blow. anyways i ended up my search with no concrete evidence to get notified while TerminateProcess killed my application. but thanks for your answers, really appriciate that – Ayazz Mar 15 '16 at 15:19