Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a windows service. I am trying to send mail when someone stops my service.
But when someone kills the process then OnStop event not fired.
It works when someone manually stops service through task manager.How to get mail when someone kills the process.

What I have tried:

protected override void OnStop()
{
SendMail();
}
Posted
Updated 9-Aug-16 5:23am

Killing a task is not a "normal" behaviour: it is not the same as stopping it. Killing a task is a radical OS activity which terminates threads immediately, and returns process memory to Windows - it does not attempt to do anything to the process to ensure an orderly shutdown, flush streams, or anything else - it is the operating system taking out a contract on an employee instead of giving him four weeks notice to quit!

When the user kills your process, that's it: you don't get any warning it will occur. The only way you can get your email sent is to have a separate service which monitors the original services' health and sends the email when it is terminated abnormally.
 
Share this answer
 
The only option you have is to prevent from killing the Service. It seems not to be easy, see discussion here: Create a unterminable process in Windows - Information Security Stack Exchange[^]

A weak solution is to have a guard Service which observes the existence of your main Service.

I hope this helps.
 
Share this answer
 
Comments
Richard Deeming 9-Aug-16 11:32am    
Sounds like a good way to get your service marked as malware!

There are very few legitimate cases where you would need a process that couldn't be killed by a local administrator.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900