Detecting process termination from task manager in C #

I have an application in C # and you want to track its current status, that is, its launch or closure. So far, I can track the status if the application exits during a runtime error or any other in the application exit codes.

But I can not register the status of the application when it is forcibly closed by the task manager.

Is there a way to track End Process events? or do I need to create a separate service to monitor my application (which, in my opinion, will be a waste of resources)?

A Brief History: The application is a WCF subscriber. An application designed to run as a background process and receive data from a server. I do not want to use the Windows service, as I am developing a graphical interface that allows clients to send requests to the server for specific data.

This application is associated with another application that monitors the subscriber, regardless of whether he is connected or not in the network.

So he was looking for a way to track the quick shutdown of the application and notify users accordingly.

+4
source share
2 answers

I am sure that I had seen such a question on SO before, so this is probably a duplicate, but I cannot find it, therefore ...

Here is a quick overview:

  • If you select a process on the "Processes" tab and click "End Process", the process will be killed using TerminateProcess() - you cannot even intercept this from your process, and even more so, prevent it.

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

    • Console applications dispatch the CTRL_CLOSE_EVENT event.
    • Windows / GUI applications send a WM_CLOSE message.

    You could answer (or handle logging, etc.) both.

Refresh : Love the source again.

Update 2 : found duplicate and vote to close. At the moment, I am leaving this answer.

+5
source

Event Process.WaitForExit () and Process.Exited. see here for more information

-1
source

Source: https://habr.com/ru/post/1432877/


All Articles