tags:

views:

123

answers:

5

I have a simple .exe that needs to be running continuously.

Unfortunately, sometimes it crashes unexpectedly, and there's nothing that can be done for this.

I'm thinking of like a C# program that scans the running application tree on a timer and if the process stops running it re-launches it... ? Not sure how to do that though....

Any other ideas?

A: 

easiest way is to have you program see if an instance of itself is running and exit if it is. Set up a scheduled task to run it every couple of minutes.

class Program
{
    static void Main(string[] args)
    {
        if (IsRunning())
        {
            return;
        }
        else
        {
            for (int x = 0; x < 10; x++)
            {
                //Do Stuff
                System.Threading.Thread.Sleep(1000);
            }
        }
    }

    private static bool IsRunning()
    {

        Process[] P = Process.GetProcessesByName( Process.GetCurrentProcess().ProcessName  ) ;
        return P.Count() > 1;
    }
}
rerun
Searching processes by name is nowhere near safe. You can't be certain that what you find is what you searched for.
n0rd
+2  A: 

It's fairly easy to do that, but the "crashes unexpectedly, and there's nothing that can be done for this" sounds highly suspect to me. Perhaps you mean the program in question is from a third party, and you need to work around problems they can't/won't fix?

In any case, there's quite a bit of sample code to do exactly what you're talking about.

Jerry Coffin
have you tried that?? looks interesting and probably should fit on my project.Have you tried this on it's own process and not another process?
Nullstr1ng
Personally, no -- I prefer to eliminate the crashing problem over working around it.
Jerry Coffin
+1  A: 

The first solution would be to fix your EXE, so it does not crash. If you can not fix it now, you probably need to add exception handling, so you can catch the exception, and not close the EXE.

Second solution is to write simple guard programm that will start your simple .exe and will monitor specific process handle. It will restart your program when it closes.

Kirill V. Lyadvinsky
what the guard program crashes too?Would be nice if both of that exe is monitoring each other.
Nullstr1ng
@Nullstr1ng: The guard program should be written correctly such that it doesn't crash.
James McNellis
anyway, he really have to fix those crashes or just trap it all and make a log file with error details. @James, just a precaution probably.. let's say, they found the guard program and intentionally terminate it.
Nullstr1ng
A: 

One trick occasionally employed by malware in days past was to have two processes that each monitor the currently running processes and restart the other process if it is terminated.

The System.Diagnostics namespace has classes which can help, particularly "Process".

For example

static Process[] Process.GetProcesses()

returns a list of all the currently running processes.

If your other process is not in this list, you just restart it with, for example

Process.Start()
Spike
A: 

Your program needs to initially start your target process itself (with Process.Start), then simply wait for it to terminate (with WaitForExit on object that is returned by Process.Start()). After that whole procedure is repeated.

This way you'd be sure that you are watching the process you are interested in, and you don't need to poll process list at all.

Process.Start() and WaitForExit() usage example.

n0rd