Click here to Skip to main content
15,885,212 members
Articles / Programming Languages / C#
Article

How to know when the Task Manager kills your app

Rate me:
Please Sign up or sign in to vote.
1.35/5 (12 votes)
17 Sep 2007CPOL1 min read 59.7K   3.7K   16   8
Use a forked process to check when the Task Manager kills your app.

Screenshot - Diagram.jpg

Introduction

Hi everyone! This is my first CodeProject article! Whoa!

Today, I bring a solution to the problem of finding out in code when the Task Manager closes your application. I'm sure many of you out there have already figured out this one, but I couldn't find a good C# code for this, so here it is.

Background

On the FormClosing event of a Form, there is a FormClosingEventArgs which has a property that states the reason of the closing, named CloseReason. TaskManagerClosing is one of the possible reasons it gives.

But sadly, this method will only work if the user tries to close the application using the End Task button of the Applications tab. If, on the contrary, the user physically kills the process from the Processes tab, then the application dies a good death and never raises the FormClosing event (it's automatically removed from the OS's process table).

Using the code

I chose the "forking a process" pattern to avoid the creation of a second assembly. Although certainly, this, in a way, makes your code look kind of bloated.

Another solution would be to create a second executable, or perhaps a library, and have your helper code run there, but for simplicity's sake, this method is good enough.

This example creates a forked process that permanently checks the main process, and when it detects the main process's death, goes to check for the Task Manager being open, and then takes the desired action.

Actually, there must be a better way to check for the kill, maybe a catching a message on the Task Manager, or hooking to Task Manager. But, that would be an expert solution.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Unknown
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionwindows 10 not detect issue Pin
Member 1415992215-May-19 19:51
Member 1415992215-May-19 19:51 
Questioncan fix that ? Pin
1987198669-Nov-14 0:49
1987198669-Nov-14 0:49 
GeneralMy vote of 1 Pin
theonlyavenger11-Aug-11 21:54
theonlyavenger11-Aug-11 21:54 
QuestionProcess Handles Pin
Captain 247-Sep-09 23:54
Captain 247-Sep-09 23:54 
Questionhow to hide process of checker Pin
liangpz_200017-Sep-07 21:22
liangpz_200017-Sep-07 21:22 
AnswerRe: how to hide process of checker Pin
Nelson Melo18-Sep-07 5:49
Nelson Melo18-Sep-07 5:49 
GeneralProgramatically Pin
Emil - Gabriel17-Sep-07 20:07
Emil - Gabriel17-Sep-07 20:07 
GeneralRe: Programatically [modified] Pin
Nelson Melo18-Sep-07 5:51
Nelson Melo18-Sep-07 5:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.