-1

I developed an application in C#. Is there any way that if a user kill the process ,in task manager, of my application then the application will start again automatically i searched a lot for such type of events that should be fired when the process will be killed manually from task manager thanks

  • 2
    Ultimately, if the user tries hard enough, they will be able to kill your application. What are you trying to achieve? A Windows Service might be a route to consider. – KingCronus Apr 30 '13 at 10:09
  • 2
    Are you sure that's wise? If I was running an application which kept restarting every time I killed it I would get rather enraged. – Paulie Waulie Apr 30 '13 at 10:09
  • @PaulieWaulie Chrome achieves this kind of trick by having lots of processes that seem to restart each other if any one fails... and yes, it makes me enraged. – Oliver Apr 30 '13 at 10:10
  • I agree with all of the above, that sounds really very annoying. However, if you want to do it look into windows services - that said, from what I've read that may not work fully if the user is on Vista/7/8 – Kobunite Apr 30 '13 at 10:12
  • but there should be a such type of event that will be fired when the process will be killed manually from task manager – Usman Raza Attar Apr 30 '13 at 10:13
  • is there any way to prevent my application from dieing – Usman Raza Attar Apr 30 '13 at 10:15
  • @UsmanRazaAttar no such event is fired. Your process is killed pretty much immediately, so you'd have no way of responding to the event even if there was one. It's not like Linux where you get a SIGTERM you can intercept. – PhonicUK Apr 30 '13 at 10:16
  • @UsmanRazaAttar `is there any way to prevent my application from dieing`? fortunately, not :) – Zdeslav Vojkovic Apr 30 '13 at 10:18
  • If the user is unprivileged you can simply set ACLs that prevent them from killing your process. If the user/instance of task manager is privileged that won't work. – CodesInChaos Apr 30 '13 at 10:20
  • There is [`RegisterApplicationRestart`](http://msdn.microsoft.com/en-us/library/windows/desktop/aa373347(v=vs.85).aspx), but I don't know if it covers this form of process termination. – CodesInChaos Apr 30 '13 at 10:24

2 Answers2

5

If the user kills your process - that's pretty much it. You won't get any events, nothing.

What you'd need to do is have a second process running that was monitoring the first, occasionally polling the list of running processes and re-starting the first in the event that it's stopped. Alternatively you could have them use IPC to do occasional heartbeats to avoid looking at the entire process list.

And of course if the user kills the monitor process first, then you don't really get anywhere unless both processes monitor each other and start whichever one is missing, but now you're just going around in circles.

Generally though this is a bad idea. If the user wants to stop your process, you should let them. Why would you want to stop them?

PhonicUK
  • 13,486
  • 4
  • 43
  • 62
  • But if i will to start another process to monitor the first one then functionality will be duplicated – Usman Raza Attar Apr 30 '13 at 10:17
  • Is there any way to start two processes of same application but with different names so that i can handle this problem – Usman Raza Attar Apr 30 '13 at 10:19
  • There's no need to start exactly the same process twice, just write a second small application whose sole purpose is to do the monitoring. – PhonicUK Apr 30 '13 at 10:24
  • Dear i want this only in a single application. kindly tell me that Is there any way to start two processes of same application but with different names – Usman Raza Attar Apr 30 '13 at 10:26
  • There isn't really a sensible way of doing that. You could make the process copy itself into another file with a different name and start that one, but you would have 2 separate files that way. – PhonicUK Apr 30 '13 at 10:27
  • @UsmanRazaAttar What do you mean by different names? Unless you do some evil stuff, processes are listed by the filename of their main executable. But I don't see why you need two different names. You can simply start two instances of the same process but with different command line arguments for the watchdog. – CodesInChaos Apr 30 '13 at 10:28
3

The only solution i see is another process that watches the primary process and restarts it. I would use a Mutex in the primary process and watch that Mutex in the watch process. A Released Mutex means the primary process was stoppped.

/// <summary>
/// Main Program.
/// </summary>
class Program
{
    static void Main(string[] args)
    {
        // Create a Mutex which so the watcher Process 
        using (var StartStopHandle = new Mutex(true, "MyApplication.exe"))
        {
            // Start the Watch process here. 
            Process.Start("MyWatchApplication.exe");                

            // Your Program Code...
        }
    }
}

In the watch process:

/// <summary>
/// Watching Process to restart the application.
/// </summary>
class Programm
{
    static void Main(string[] args)
    {
        // Create a Mutex which so the watcher Process 
        using (var StartStopHandle = new Mutex(true, "MyApplication.exe"))
        {
            // Try to get Mutex ownership. 
            if (StartStopHandle.WaitOne())
            { 
                // Start the Watch process here
                Process.Start("MyApplication.exe");

                // Quit after starting the Application.
            }
        }
    }
}
Mecaveli
  • 1,507
  • 10
  • 16