我现在有服务A, 服务B, 服务C我想用服务A定时去检测 服务B 和 服务C, 发现服务停止即重新开启服务。不知道有没有办法可以做到? 

解决方案 »

  1.   

    A服务中,加定时器timer控件,Tick事件中检测服务B、服务C是否运行,没运行启动;
      

  2.   

                        ServiceController sc = new ServiceController("servicename");
                        ServiceControllerStatus ls = sc.Status;
                        switch (sStatus)
                        {
                            case "Stopped":
                                if (ls == ServiceControllerStatus.Running || ls == ServiceControllerStatus.Paused)
                                {
                                    sc.Stop();
                                }
                                break;
                            case "Paused":                            if (ls == ServiceControllerStatus.Running)
                                {
                                    sc.Pause();
                                }
                                break;
                            case "Running":                            if (ls == ServiceControllerStatus.Stopped)
                                {
                                    sc.Start();
                                }
                                else if (ls == ServiceControllerStatus.Paused) 
                                {
                                    sc.Continue();
                                }
                                break;
                        }
      

  3.   

                        ServiceController sc = new ServiceController("servicename"); 
                        ServiceControllerStatus ls = sc.Status; 
                        switch (sStatus) 
                        { 
                            case "Stopped": 
                                if (ls == ServiceControllerStatus.Running || ls == ServiceControllerStatus.Paused) 
                                { 
                                    sc.Stop(); 
                                } 
                                break; 
                            case "Paused":                            if (ls == ServiceControllerStatus.Running) 
                                { 
                                    sc.Pause(); 
                                } 
                                break; 
                            case "Running":                            if (ls == ServiceControllerStatus.Stopped) 
                                { 
                                    sc.Start(); 
                                } 
                                else if (ls == ServiceControllerStatus.Paused) 
                                { 
                                    sc.Continue(); 
                                } 
                                break; 
                        } 
      

  4.   

    在程序里添加一个timer控件,然后在该控件的属性的Interval设置每隔多长时间执行一次,这里的单位是毫秒,然后双击timer控件,在里面写代码:检测有没有服务B和C的进程,有的话就什么都不执行,没有的话就用Process.Start("服务名")启动起来。关于进程的操作,可以参考下列文章:http://blog.csai.cn/user1/22158/archives/2007/16631.html
      

  5.   


    建议不要这样做。1、服务作为长时间运行的程序,本身应该是健壮的。用守护进程的方式来解决服务不稳定,只会带来新的问题。2、对于关键服务,Windows已经提供了恢复功能,允许在服务出错的情况下重启服务,甚至重启系统。