Home Ask Login Register

Developers Planet

Your answer is one click away!

daniele3004 February 2016

How to prevent the Kill() process by another?

In C# it's possible to block the Kill() process by another(user or program) ??

I want to close my application only by the "Close Button" and block other way.


Carlos February 2016

I've used C# for years and I've never come across such a mechanism. You would need some way to tell the OS that the current process is somehow protected from being killed. It's maybe more of an OS question than a language question, actually.

Miquel Coll February 2016

The only way to do it is to create a Windows Service instead of a simple Process. This way, I think you could tell in the Windows services tab to always have it active and execute it as much as you would like. For instance:

    protected override void OnStart(string[] args)
        timer = new Timer();
        timer.Interval = 12000; //Execute the process every 12 seconds
        timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Tick);
        timer.Enabled = true;

    private void timer_Tick(object sender, ElapsedEventArgs e)
        try {
            Runner.WriteErrorLog("Timer ticked and some job has ben called!");
            Runner.WriteErrorLog("Job done!!");
        catch (Exception ex)
            Runner.WriteErrorLog("ERROR!!! " + ex.Message);

This WOULD NOT prevent the termination of your process but it would enable you to read the log and find if it has been terminated and re-launch it.

Post Status

Asked in February 2016
Viewed 1,493 times
Voted 11
Answered 2 times


Leave an answer

Quote of the day: live life