7

I have an application in C#, and would like to monitor its current status, i.e. whether its running or closed. So far i am able to track the status if the application exits during a run time error, or any other in application exit codes.

But i am unable to log the application status when it is force closed by task manager.

Is there a way to monitor End Process events? or do i have to design a separate service to keep a watch on my application (which i find would be a waste of resources)?

Brief History: The application is a WCF subscriber Application that is meant to run as a background process and receive data from the server. I don't want to use a windows service as i am designing a GUI which allows the clients to send requests to the server for specif data.

This application is linked to another application which keeps track of the subscriber whether, the subscriber is online or offline.

Thus was looking for a way to track abrupt application close and notify the users accordingly.

Kush
  • 245
  • 1
  • 3
  • 11
  • 1
    I don't understand your question. You want your application to log when it's forcibly terminated? You can't do that, your application is no longer running, you just terminated it. –  Sep 07 '12 at 06:54
  • possible duplicate of [Prevent process from being closed in task manager](http://stackoverflow.com/questions/9754389/prevent-process-from-being-closed-in-task-manager) – Christian.K Sep 07 '12 at 06:57
  • Sorry For the vague question... Let me add a brief history to the Question of exactly what i want to do – Kush Sep 07 '12 at 09:33
  • Why not just make your GUI a WCF client which can connect to the local service, then you can have all the benifits of the service route but also have a client GUI. – KingCronus Sep 07 '12 at 09:41
  • I am actualy very new at WCF, so still learning. Let me try with the WCF client approach. Thanks – Kush Sep 07 '12 at 09:49
  • @KingCronus - How do i create a WCF client? is there any tutorial i can have a look at? Thanx – Kush Sep 07 '12 at 10:02
  • User1559231, that is quite a generic question, and might be better suited to being posted as a seperate question to this one. – KingCronus Sep 07 '12 at 10:03
  • possible duplicate of [Handling end process of a windows app](http://stackoverflow.com/questions/838261/handling-end-process-of-a-windows-app) – Sakthivel Mar 10 '14 at 15:35

2 Answers2

8

I'm sure I have seen a question like this on SO before, so this is probably a duplicate, but I cannot find it, so...

Here is a short wrapup:

  • If you select a process in "Processes" tab and click "End Process" the process is being killed using TerminateProcess() - there is no way you can even intercept that from your process, let alone, prevent it.

  • If you select an "application" from the "Applications" tag and click "End Task", what happens depends on the type of application:

    • Console Applications are being send a CTRL_CLOSE_EVENT event.
    • Windows/GUI Applications are being send a WM_CLOSE message.

    You could respond (or handle with logging, etc.) both.

Update: Just fond the source again.

Update 2: found a/the duplicate and voting to close. I leave this answer here for now.

Christian.K
  • 47,778
  • 10
  • 99
  • 143
  • Thanks for the information. Ill have to look for another way to monitor the status of the application.. Thankx – Kush Sep 07 '12 at 09:43
-2

Process.WaitForExit() and Process.Exited event. see here for more info

aiodintsov
  • 2,545
  • 15
  • 17