简体   繁体   English

监视过程始终在运行

[英]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? 谁知道我该如何使用C#编写Monitor程序来控制应用程序始终运行? 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. 我需要它是一个双监视器应用程序,我将解释:我有一个应用程序Ap1,它必须控制始终启动的Ap2进程,而Ap2必须控制它始终启动的Ap1进程。 In resume, if I kill Ap1 process the Ap2 application should start Ap1 immediatelly (and vice versa if Ap2 die). 作为恢复,如果我终止了Ap1进程,则Ap2应用程序应立即启动Ap1(如果Ap2死了,反之亦然)。

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 . 您可以使用Process.GetProcessesByName

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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