简体   繁体   中英

Monitoring process to be always running

Anybody knows how I could make a Monitor program in C# to control that an application will always be running? That I need it's a double monitor application, I will explain: I have a application Ap1 that have to control that Ap2 process it's always started, and Ap2 have to control that Ap1 process it's always started. In resume, if I kill Ap1 process the Ap2 application should start Ap1 immediatelly (and vice versa if Ap2 die).

This the code that I'm developing but didn't work, I don't know but when I kill the program monitored no started again.

        public void Monitor()
        {
            Console.WriteLine("Monitoring {0} process...", processname);

            while (IsProcessRunning() == true)
            {
                Process[] runningNow = Process.GetProcesses();

                foreach (Process process in runningNow)
                {
                    if (process.ProcessName == processname)
                    {
                        System.Threading.Thread.Sleep(1000);
                        Console.WriteLine("Process:{0} is running actually", process.ProcessName);
                    }
                    else { /* Provide a messagebox. */ }
                }

                // Sleep till the next loop
                Thread.Sleep(intInterval);
            }



            while (IsProcessRunning() != true)
            {
                ProcessMonitor proc = new ProcessMonitor("ConsoleApplication1", 1000);//Check if is running each 1 second
                Console.WriteLine("Process:{0} is NOT running actually", processname);
                //Application folder of exe
                String applicationFolder = "C:\\App";

                //Get the executable file
                String procPath = applicationFolder + @"\Ap1.exe";

                Console.WriteLine("Running {0} process...", proc.Name);
                //Lauch process
                Process p = Process.Start(procPath);
                Console.WriteLine("Process running {0} OK", proc.Name);
                //p.WaitForExit(10000);
            }
        }

And the main program:

static void Main(string[] args)
    {
        ProcessMonitor proc = new ProcessMonitor("ConsoleApplication1", 1000);//Check if is running each 1 second
        if (proc.IsProcessRunning() != true)
        {
            Console.WriteLine("{0} is not running.", proc.Name);

            //Application folder of exe
            String applicationFolder = "C:\\App";

            //Get the executable file
            String procPath = applicationFolder + @"\Ap1.exe";

            Console.WriteLine("Running {0} process...", proc.Name);
            //Lauch process
            Process p = Process.Start(procPath);
            Console.WriteLine("Process running {0} OK", proc.Name);
            //p.WaitForExit(10000);
        }
        else
        {
            proc.Monitor();
        }

        proc.FreezeOnScreen(); 
    }

You could set up a task in the task scheduler that launches your program at a set interval (say every half hour). I believe you can set it to not start the task if there is already an instance running. (Correct me if I am wrong)

We needed this once and simply monitored the process list periodically, checking for the name of the process. If the process wasn't present for a given amount of time, we restarted it. You can use Process.GetProcessesByName .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM