Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# catch kill command

Tags:

windows

kill

how can I catch windows taskkill command in order to prevent it?

like image 902
MBZ Avatar asked Dec 23 '22 00:12

MBZ


2 Answers

You cannot prevent your process being killed. The only way to keep a program alive is to have a second Watchdog application that is constantly pooling that process to ensure it's running.

So, you have two Applications; AppA (Main) and AppB (Watchdog).

AppA checks if AppB is running. If not, AppA starts AppB.

AppB checks if AppA is running. If not, AppB starts AppA.

But this is usually a design for a malicious process, so I hope you're not doing anything untoward.

You can see the following events in the message queue when End process is clicked:

WM_CLOSE // UI app

CTRL_CLOSE_EVENT // Console app

But if your application does not close in a timely manner, then the kill command will be sent, which cannot be captured.

like image 126
djdd87 Avatar answered Dec 28 '22 08:12

djdd87


I'm pretty sure it is not possible, otherwise a single application could stop windows from ever shutting down. Even when the process is hung, killing it in task manager eventually works. If it was possible, malicous software, etc would probably have a hey-day using it - not only could they take control of various parts of your PC, they'd make sure you could never get it back again.

like image 30
Paul Hadfield Avatar answered Dec 28 '22 06:12

Paul Hadfield